0

i need to change the - of the date into / in $myDate1 . here is my code but its not working. also i need to insert / in the $myDate2 variable.

$myDate1 = "06-20-2014";
$myDate2 = "06202014";
$newDate1 = date("m/d/Y", strtotime($myDate1));
$newDate2 = date("m/d/Y", strtotime($myDate2));
echo $newDate1;
echo $newDate2;

thanks

4
Contributors
9
Replies
40
Views
3 Years
Discussion Span
Last Post by diafol
0

i think it returns the default starting date below

01/01/197001/01/1970

however if I do it in reverse its working

$myDate = "06/20/2014";
$newDate = date("m-d-Y", strtotime($myDate));
echo $newDate;

result is

06-20-2014

0

In addition, may i ask how is this possible

if ($myDate contains '-' or '/')
{
statement;
.
.
}

thanks

0

for $myDate1 its working ..thanks..how about $myDate2

Edited by lloydsbackyard

0

im almost done only this one..
how to put / if the date is like this

$myDate = "06202014";

to make it look like this

06/20/2014

0

$newDate = substr($myDate,0,2).'/'.substr($myDate,2,2).'/'.substr($myDate,4,6);

Edited by Dani: Formatting fixed

0

I know it's solved, but this may be useful...

function safeDate($date)
{
    return preg_replace('/(\d{2})[-]?(\d{2})[-\/]?(\d{4})/','$1/$2/$3',$date);
}

$dateArray = array('01202014', '01-20-2014', '01/20/2014');

foreach($dateArray as $dateItem)
{
    echo $dateItem . ' => ' . safeDate($dateItem) . '<br>';  
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.