PHP strtotime() "noon" must go before date

Martin Burch

The PHP strtotime() function claims to be able to handle "noon" and "midnight" but it's not working for me.

$noon_timestamp = strtotime("August 3 noon");
print date("Y-m-d H:i:s", $noon_timestamp);
1969-12-31 19:00:00

However, this does work:

$twelve_timestamp = strtotime("August 3 12:00 PM");
print date("Y-m-d H:i:s", $twelve_timestamp);
2015-08-03 12:00:00

I found this did work:

$noon_timestamp = strtotime("noon August 3");
print date("Y-m-d H:i:s", $noon_timestamp).PHP_EOL;
2015-08-03 12:00:00

But I'm not sure why I need to move "noon" to before the date. Seems like it should work either way. I know users will try both methods.

EDIT: I think I've found a relevant note in the PHP docs under Relative Formats:

Exceptions to this rule are: "yesterday", "midnight", "today", "noon" and "tomorrow". Note that "tomorrow 11:00" and "11:00 tomorrow" are different. Considering today's date of "July 23rd, 2008" the first one produces "2008-07-24 11:00" where as the second one produces "2008-07-24 00:00". The reason for this is that those five statements directly influence the current time.

I don't understand the last sentence. "Directly influence the current time" ? That seems irrelevant to the order. "tomorrow 11:00" and "11:00 tomorrow" mean the same thing in English.

Martin Burch

I'm not sure this is a general solution, but I made this function:

function arrange_timestring($timestring) {
    $problematic_keywords = array("noon","midnight");
    $keyword_counter = 0;
    foreach($problematic_keywords as $keyword) {
        if (strpos($timestring,$keyword) !== false) {
            $keyword_counter += 1;
            if (strpos($timestring,$keyword) > 0) {
                $timestring = str_replace($keyword, "", $timestring);
                $timestring = $keyword." ".$timestring;
            }
        }
    }
    if ($keyword_counter > 1) {
        return Null; //strings shouldn't have both noon and midnight
    } else {
    return $timestring;
    }
}

Then my code works as expected:

$noon_timestamp = strtotime(arrange_timestring("August 3 noon"));
$twelve_timestamp = strtotime(arrange_timestring("August 3 12:00 PM"));
print date("Y-m-d H:i:s", $noon_timestamp);
2015-08-03 12:00:00
print date("Y-m-d H:i:s", $twelve_timestamp);
2015-08-03 12:00:00

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP date format and strtotime

From Dev

Must be a unixtime or a date/time representation parseable by strtotime

From Dev

Must be a unixtime or a date/time representation parseable by strtotime

From Dev

PHP date conversion using strtotime

From Dev

Formatting date using PHP strtotime

From Dev

Check a date interval with strtotime in php

From Dev

format of date string for strtotime() function in php

From Dev

String date to time in PHP - strtotime "loses hours"

From Dev

PHP date & strtotime adding 12 minutes?

From Dev

PHP Converting Integer to Date, reverse of strtotime

From Dev

PHP: Add some milliseconds to a date() with strtotime()

From Dev

PHP strtotime() with date() issue with year field

From Dev

strtotime is not working proper in php for future date?

From Dev

Php: Change language in date/strftime/strtotime

From Dev

PHP: strtotime revered return strange date?

From Dev

PHP: Add some milliseconds to a date() with strtotime()

From Dev

PHP date returning blank for Year "2090" with strtotime

From Dev

strtotime is not working proper in php for future date?

From Dev

format of date string for strtotime() function in php

From Dev

how to overwrite date to 1st of month php strtotime and date

From Dev

PHP Countdown timer for promotion using strtotime("future date/time") - strtotime("now") = countdown?

From Dev

PHP Countdown timer for promotion using strtotime("future date/time") - strtotime("now") = countdown?

From Dev

PHP strtotime error when convert some date string to datetime value

From Dev

PHP parse date with Timezone+Offset using strtotime()

From Dev

STRTOTIME to find current week, date for Monday and Saturday, PHP

From Dev

Using strtotime() PHP and revert back trough gmdate() is not returning same date

From Dev

STRTOTIME take date and data from mysql and concatenate them on php

From Dev

Using Moment.js like PHP's date and strtotime

From Dev

How does PHP determine date format for a strtotime() call

Related Related

  1. 1

    PHP date format and strtotime

  2. 2

    Must be a unixtime or a date/time representation parseable by strtotime

  3. 3

    Must be a unixtime or a date/time representation parseable by strtotime

  4. 4

    PHP date conversion using strtotime

  5. 5

    Formatting date using PHP strtotime

  6. 6

    Check a date interval with strtotime in php

  7. 7

    format of date string for strtotime() function in php

  8. 8

    String date to time in PHP - strtotime "loses hours"

  9. 9

    PHP date & strtotime adding 12 minutes?

  10. 10

    PHP Converting Integer to Date, reverse of strtotime

  11. 11

    PHP: Add some milliseconds to a date() with strtotime()

  12. 12

    PHP strtotime() with date() issue with year field

  13. 13

    strtotime is not working proper in php for future date?

  14. 14

    Php: Change language in date/strftime/strtotime

  15. 15

    PHP: strtotime revered return strange date?

  16. 16

    PHP: Add some milliseconds to a date() with strtotime()

  17. 17

    PHP date returning blank for Year "2090" with strtotime

  18. 18

    strtotime is not working proper in php for future date?

  19. 19

    format of date string for strtotime() function in php

  20. 20

    how to overwrite date to 1st of month php strtotime and date

  21. 21

    PHP Countdown timer for promotion using strtotime("future date/time") - strtotime("now") = countdown?

  22. 22

    PHP Countdown timer for promotion using strtotime("future date/time") - strtotime("now") = countdown?

  23. 23

    PHP strtotime error when convert some date string to datetime value

  24. 24

    PHP parse date with Timezone+Offset using strtotime()

  25. 25

    STRTOTIME to find current week, date for Monday and Saturday, PHP

  26. 26

    Using strtotime() PHP and revert back trough gmdate() is not returning same date

  27. 27

    STRTOTIME take date and data from mysql and concatenate them on php

  28. 28

    Using Moment.js like PHP's date and strtotime

  29. 29

    How does PHP determine date format for a strtotime() call

HotTag

Archive