Question

$date_string = "1 October, 2013";
$date_time = strtotime($date_string); //evaluates to "1412208780"
$date_string_again = date("m/d/y", $date_time); //evaluates to "10/01/14" .. should be "10/01/13"

Why is my code giving me the wrong year? How do I fix it?

Was it helpful?

Solution 2

Indeed, the comma is throwing it off. Remove it!

$date_string = "1 October, 2013";
$date_string = str_replace(',','',$date_string);
$date_time = strtotime($date_string); 
$date_string_again = date("m/d/y", $date_time);

OTHER TIPS

Make use of DateTime::createFromFormat

<?php
$dt = '1 October, 2013';
$date = DateTime::createFromFormat('j F, Y', $dt);
echo $date->format('m/d/Y'); //"prints" 10/01/2013

Your string is not a valid format that strtotime() recognizes, see the manual for accepted formats.

If the format is fixed like this, you could use DateTime::createFromFormat to convert your date to a DateTime object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top