Pergunta

$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?

Foi útil?

Solução 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);

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top