I have a date like this: 2. Februar 2012
I want to have it converted to 2012-02-02, so I wrote this code:
$date = '2. Februar 2012';
$date = date('Y-m-d', $date);
The $date var is either empty or 1970-01-01 afterwards, whats wrong or missing?
Note: The date is in German format, so its not February, its Februar. I get the date from a date picker that way.
Thanks!
use strtotime
putenv('LC_ALL=de_DE');
putenv('LANG=de');
setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'deu_deu');
$date = '2. Februar 2012';
$date = date('Y-m-d', strtotime($date));
You can use *strtotime and also need to pass the valid date format in strtotime function as your $date
variable is not having valid format.
You have .
and misspelled month name. You have to clear those before passing in strtotime
. I have used str_replace
for this.
$date = '2. Februar 2012';
$date = date('Y-m-d', strtotime(str_replace('Februar','february',str_replace('.','', $date))));
Use strtotime()
.
strtotime("2 February 2012") will return the unix timestamps.
mktime(0, 0, 0, 2, 2, 2012) will return the same unix timestamps.
If you can run
$ts = mktime(0, 0, 0, 2, 2, 2012);
echo date("Y-m-d H:i:s", $ts); // output 2012-02-02 00:00:00
You can run the following too
$ts = strtotime("2 February 2012");
echo date("Y-m-d H:i:s", $ts); // output 2012-02-02 00:00:00
use like this:
$date = '2. February 2012';
$date = strtotime($date);
$date = date('Y-m-d', $date);
echo $date;
date()
function in php expects first parameter as string. It's ok in your example. Seconds parameter is optional and it expected to be integer with timestamp you want to convert.
Reference: http://php.net/manual/en/function.date.php