-1

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.

ami_nedved
  • 17
  • 7

5 Answers5

2

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").

lsowen
  • 3,728
  • 1
  • 21
  • 23
1

try this

<?php echo date("Y-m-d",strtotime(str_replace('/', '-',  YOUR DATE )))); ?>
Maninderpreet Singh
  • 2,569
  • 2
  • 17
  • 31
0

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"));
Swapnil
  • 592
  • 3
  • 13
0

Here is the simple solution

$date = '27/03/2015';
$date = str_replace('/', '-', $date);
echo date('Y-m-d', strtotime($date));
Krishna38
  • 707
  • 1
  • 16
  • 43
0

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)); 
?>
t_mo_t
  • 67
  • 8
  • While this may answer the question it’s always a good idea to put some text in your answer to explain what you're doing. Read [how to write a good answer](http://stackoverflow.com/help/how-to-answer). – Jørgen R Apr 09 '15 at 12:33