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;

?>
1713101956

2 · baseTimestamp

<?

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

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

echo $return;

?>
1713101956

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";
}

?>
1713101956: 2024-04-14 15 13:39:16: now
1713101955: 2024-04-14 15 13:39:15: -1 second
1713101896: 2024-04-14 15 13:38:16: -1 minute
1713098356: 2024-04-14 15 12:39:16: -1 hour
1713015556: 2024-04-13 15 13:39:16: -1 day
1712497156: 2024-04-07 14 13:39:16: -1 week
1710423556: 2024-03-14 11 13:39:16: -1 month
1681479556: 2023-04-14 15 13:39:16: -1 year
1678106295: 2023-03-06 10 12:38:15: -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";
}

?>
1713101956: 2024-04-14 15 13:39:16: now
1713101957: 2024-04-14 15 13:39:17: 1 second
1713102016: 2024-04-14 15 13:40:16: 1 minute
1713105556: 2024-04-14 15 14:39:16: 1 hour
1713188356: 2024-04-15 16 13:39:16: 1 day
1713706756: 2024-04-21 16 13:39:16: 1 week
1715693956: 2024-05-14 20 13:39:16: 1 month
1744637956: 2025-04-14 16 13:39:16: 1 year
1747924817: 2025-05-22 21 14:40:17: 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";
}

?>
1713101956: 2024-04-14 15 13:39:16: now
1712966400: 2024-04-13 15 00:00:00: yesterday
1713052800: 2024-04-14 15 00:00:00: today
1713052800: 2024-04-14 15 00:00:00: midnight
1713096000: 2024-04-14 15 12:00:00: noon
1713139200: 2024-04-15 16 00:00:00: tomorrow
1713095100: 2024-04-14 15 11:45:00: front of 12
1713096900: 2024-04-14 15 12:15:00: back of 12
1713015556: 2024-04-13 15 13:39:16: previous day
1713188356: 2024-04-15 16 13:39:16: next day
1713015556: 2024-04-13 15 13:39:16: 1 day ago
1713188356: 2024-04-15 16 13:39:16: -1 day ago
1711978756: 2024-04-01 14 13:39:16: first day of
1714484356: 2024-04-30 18 13:39:16: 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 Monday the 15th of April 2024 at 01:39:16 PM
HomeMenu