2

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!

PeeHaa
  • 69,318
  • 57
  • 185
  • 258
EOB
  • 2,887
  • 17
  • 42
  • 68
  • check this link http://stackoverflow.com/questions/9166656/php-convert-a-string-that-contains-a-date-in-this-format-2-february-2012-to-mkt – Ghostman Feb 28 '12 at 11:52
  • read this: http://php.net/manual/en/datetime.formats.date.php – EscoMaji Feb 28 '12 at 13:04

5 Answers5

2

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))));
Shakti Singh
  • 81,083
  • 20
  • 131
  • 150
  • 2
    No, its not mispelled, its German ... and in German its Februar. The date is coming from a calendar control that way. – EOB Feb 28 '12 at 11:56
  • @EOB: That it is not a valid date format that strtotime accept. You have to change it to `february` or a valid date format. You can see here http://www.php.net/manual/en/datetime.formats.date.php – Shakti Singh Feb 28 '12 at 11:58
  • Yeah, that might be, but what about local dates? There must be a solution? – EOB Feb 28 '12 at 12:00
  • @EOB: I don't think there is, until you use a valid date format listed on PHP manual page. There is also a PHP [DateTime class](http://php.net/manual/en/class.datetime.php) and I don't think which help you much until you do agree with a valid date format. – Shakti Singh Feb 28 '12 at 12:05
2

use like this:

$date = '2. February 2012';
$date = strtotime($date);
$date = date('Y-m-d', $date);

echo $date;
Arfeen
  • 2,535
  • 4
  • 28
  • 48
0

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));
Book Of Zeus
  • 48,853
  • 18
  • 173
  • 169
0

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 
Brian Mains
  • 50,194
  • 35
  • 142
  • 253
Ghostman
  • 5,918
  • 9
  • 33
  • 52
0

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

Sergey P. aka azure
  • 3,320
  • 1
  • 25
  • 22