2

I'm trying to create a DateTime objet but with a specific DateTimeZone (the server could have it own DateTimeZone). Suppose the following string:

$textoFecha = '15-30-10-2012'; //H-d-m-Y

If I try to do this:

$fecha = DateTime::createFromFormat('H-d-m-Y', $textoFecha);
$fecha->setTimezone(new DateTimeZone("America/Costa_Rica"));

The result of make echo $fecha->format('H-d-m-Y') is:

19-30-10-2012

My question is, how can I set the DateTimeZome but keeping the giving values? (15-30-10-2012)

hakre
  • 193,403
  • 52
  • 435
  • 836
manix
  • 14,537
  • 11
  • 70
  • 107

1 Answers1

2

You have to specify the timezone in DateTime::createFromFormat, like this:

$textoFecha = '15-30-10-2012'; //H-d-m-Y
$fecha = DateTime::createFromFormat('H-d-m-Y', $textoFecha, new DateTimeZone("America/Costa_Rica"));
echo $fecha->format('H-d-m-Y'); // prints 15-30-10-2012

Working example: http://codepad.viper-7.com/dqHDLW

Klemen Tusar
  • 9,261
  • 4
  • 31
  • 28