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

Recommended Answers

All 9 Replies

How is it not working? what errors?

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

In addition, may i ask how is this possible

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

thanks

Try replacing "-" with "/"
str_replace("-","/","$myDate1");

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

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

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

Member Avatar for diafol

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>';  
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.