Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

strtotime

Description

The strtotime of Date / Time for PHP parse about any English textual datetime description into a Unix timestamp.

Syntax

strtotime(
    string $datetime,
    ?int $baseTimestamp = null
): int|false

Parameters

time

A date/time string. Valid formats are explained in Date and Time Formats.

baseTimestamp

The timestamp which is used as a base for the calculation of relative dates.

Return

Returns a timestamp on success, false otherwise.

Examples

1 · datetime

<?

$datetime = "now";

$return = strtotime($datetime);

echo $return;

?>
1721264600

2 · baseTimestamp

<?

$datetime = "now";
$baseTimestamp = time();

$return = strtotime($datetime, $baseTimestamp);

echo $return;

?>
1721264600

3 · -

<?

$array =
[
    "now",
    "-1 second",
    "-1 minute",
    "-1 hour",
    "-1 day",
    "-1 week",
    "-1 month",
    "-1 year",
    "-1 year -1 month -1 week -1 day -1 hour -1 minute -1 second"
];

foreach($array as $datetime)
{
    $return = strtotime($datetime);

    $format = "Y-m-d W H:i:s";

    $date = date($format, $return);

    echo "$return: $date: $datetime\n";
}

?>
1721264600: 2024-07-18 29 01:03:20: now
1721264599: 2024-07-18 29 01:03:19: -1 second
1721264540: 2024-07-18 29 01:02:20: -1 minute
1721261000: 2024-07-18 29 00:03:20: -1 hour
1721178200: 2024-07-17 29 01:03:20: -1 day
1720659800: 2024-07-11 28 01:03:20: -1 week
1718672600: 2024-06-18 25 01:03:20: -1 month
1689642200: 2023-07-18 29 01:03:20: -1 year
1686355339: 2023-06-10 23 00:02:19: -1 year -1 month -1 week -1 day -1 hour -1 minute -1 second

4 · +

<?

$array =
[
    "now",
    "1 second",
    "1 minute",
    "1 hour",
    "1 day",
    "1 week",
    "1 month",
    "1 year",
    "1 year 1 month 1 week 1 day 1 hour 1 minute 1 second"
];

foreach($array as $datetime)
{
    $return = strtotime($datetime);

    $format = "Y-m-d W H:i:s";

    $date = date($format, $return);

    echo "$return: $date: $datetime\n";
}

?>
1721264600: 2024-07-18 29 01:03:20: now
1721264601: 2024-07-18 29 01:03:21: 1 second
1721264660: 2024-07-18 29 01:04:20: 1 minute
1721268200: 2024-07-18 29 02:03:20: 1 hour
1721351000: 2024-07-19 29 01:03:20: 1 day
1721869400: 2024-07-25 30 01:03:20: 1 week
1723943000: 2024-08-18 33 01:03:20: 1 month
1752800600: 2025-07-18 29 01:03:20: 1 year
1756173861: 2025-08-26 35 02:04:21: 1 year 1 month 1 week 1 day 1 hour 1 minute 1 second

5 · day-based

<?

$array =
[
    "now",
    "yesterday",
    "today",
    "midnight",
    "noon",
    "tomorrow",
    "front of 12",
    "back of 12",
    "previous day",
    "next day",
    "1 day ago",
    "-1 day ago",
    "first day of",
    "last day of"
];

foreach($array as $datetime)
{
    $return = strtotime($datetime);

    $format = "Y-m-d W H:i:s";

    $date = date($format, $return);

    echo "$return: $date: $datetime\n";
}

?>
1721264601: 2024-07-18 29 01:03:21: now
1721174400: 2024-07-17 29 00:00:00: yesterday
1721260800: 2024-07-18 29 00:00:00: today
1721260800: 2024-07-18 29 00:00:00: midnight
1721304000: 2024-07-18 29 12:00:00: noon
1721347200: 2024-07-19 29 00:00:00: tomorrow
1721303100: 2024-07-18 29 11:45:00: front of 12
1721304900: 2024-07-18 29 12:15:00: back of 12
1721178201: 2024-07-17 29 01:03:21: previous day
1721351001: 2024-07-19 29 01:03:21: next day
1721178201: 2024-07-17 29 01:03:21: 1 day ago
1721351001: 2024-07-19 29 01:03:21: -1 day ago
1719795801: 2024-07-01 27 01:03:21: first day of
1722387801: 2024-07-31 31 01:03:21: last day of

6 · specific

<?

$array =
[
    "01-02-03",
    "3 february 2001"
];

foreach($array as $datetime)
{
    $return = strtotime($datetime);

    $format = "Y-m-d W H:i:s";

    $date = date($format, $return);

    echo "$return: $date: $datetime\n";
}

?>
981158400: 2001-02-03 05 00:00:00: 01-02-03
981158400: 2001-02-03 05 00:00:00: 3 february 2001

7 · holidays

<?

$array =
[
    "Mother's Day" => "second sunday of may",
    "Memorial Day" => "last monday of may",
    "Father's Day" => "third sunday of june",
    "Thanksgiving Day" => "fourth thursday of november"
];

foreach($array as $key => $datetime)
{
    $return = strtotime($datetime);

    $format = "Y-m-d";

    $date = date($format, $return);

    echo "$date: $key\n";
}

?>
2024-05-12: Mother's Day
2024-05-27: Memorial Day
2024-06-16: Father's Day
2024-11-28: Thanksgiving Day

8 · escape

<?

$datetime = "next day";

$return = strtotime($datetime);

$format = 'l \t\h\e jS \o\f F Y \a\t h:i:s A';

$date = date($format, $return);

echo "\"$datetime\" is $date";

?>
"next day" is Friday the 19th of July 2024 at 01:03:21 AM
HomeMenu