I have a string value "27/03/2015" and i want to convert this string to new date format. Below is the code i am using now.
<?php echo date("Y-m-d",strtotime("27/03/2015")); ?>
But it gives a wrong output like this 1970-01-01.
I have a string value "27/03/2015" and i want to convert this string to new date format. Below is the code i am using now.
<?php echo date("Y-m-d",strtotime("27/03/2015")); ?>
But it gives a wrong output like this 1970-01-01.
It is because strtotime
isn't being able to parse your date string. Try:
<?php echo strtotime("27/03/2015"); ?>
The result should be False
. Since False
is the same as 0
, you are really running date("Y-m-d", 0)
, the result of which is "1970-01-01" (the "unix epoch").
strtotime
only recognizes certain date formats, listed here. The closest to your input format is "27-03-2015" ("Day, month and four digit year, with dots, tabs or dashes").
try this
<?php echo date("Y-m-d",strtotime(str_replace('/', '-', YOUR DATE )))); ?>
in above case / Seperator is not valid (as the date will be evaluated to Date 3 & Month 27) you can use -
echo date("Y-m-d",strtotime("27-03-2015"));
Here is the simple solution
$date = '27/03/2015';
$date = str_replace('/', '-', $date);
echo date('Y-m-d', strtotime($date));
I guess "/" is not allowed or, should I say, unrecognizable as a parameter for strtotime.
<?php
$dateString = "27/03/2015";
//now let's check if the variable has a "/" inside of it.
//If it does, then replace "/" with "-".
//If it doesnt, then go with it.
//"." also accepted for strtotime as well.
$dateString = (strpos($dateString,"/") ? str_replace("/","-",$dateString) : $dateString);
echo date("Y-m-d",strtotime($dateString));
?>