3

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
  • 71,436
  • 58
  • 190
  • 262
EOB
  • 2,975
  • 17
  • 43
  • 70
  • 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

6 Answers6

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
  • 84,385
  • 21
  • 134
  • 153
  • 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
1

old question but iv'e build a solution for comparing two dates (from german base like "9. Mai 2022") to US date format dynamically.

$post_date = get_field( "veranstaltung_datum", $veranstaltung->ID ); // e.g. "9. Mai 2022"
$post_date_month_de = preg_replace("/[^a-zA-Z]+/", "", $post_date); // isolate the month -> "Mai"
$post_date_month_en = replaceGermanMonth($post_date_month_de); // Switch replaces Month Name -> "May"
$post_date = date('Y-m-d', strtotime(str_replace($post_date_month_de, $post_date_month_en ,str_replace('.','', $post_date)))); // replace the "." and the "Mai" with "" and "May"
$post_date = strtotime($post_date); // convert in to ms since 01.01.1970
   
// get the current date in ms since 01.01.1970
$current_date = strtotime(date('Y-m-d'));

if ($current_date > $post_date) {
   // add the magic
}

Here is the switch i use to change the Month:

function replaceGermanMonth($month) {
    switch ($month) {
    case 'Januar':
        return "January";
        break;
    case 'Februar':
        return "February";
        break;
    case "März":
        return "March";
        break;
    case "April":
        return "April";
        break;
    case "Mai":
        return "May";
        break;
    case "Juni":
        return "June";
        break;
    case "Juli":
        return "July";
        break;
    case "August":
        return "August";
        break;
    case "September":
        return "September";
        break;
    case "Oktober":
        return "October";
        break;
    case "November":
        return "November";
        break;
    case "Dezember":
        return "December";
        break;
    default:
        break;
    }
}
JakobF
  • 23
  • 7
1

use like this:

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

echo $date;
Arfeen
  • 2,553
  • 5
  • 29
  • 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
  • 49,509
  • 18
  • 174
  • 171
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,520
  • 35
  • 148
  • 257
Ghostman
  • 6,042
  • 9
  • 34
  • 53
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,993
  • 1
  • 29
  • 23