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;

?>
1728297078

2 · baseTimestamp

<?

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

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

echo $return;

?>
1728297078

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

?>
1728297078: 2024-10-07 41 10:31:18: now
1728297077: 2024-10-07 41 10:31:17: -1 second
1728297018: 2024-10-07 41 10:30:18: -1 minute
1728293478: 2024-10-07 41 09:31:18: -1 hour
1728210678: 2024-10-06 40 10:31:18: -1 day
1727692278: 2024-09-30 40 10:31:18: -1 week
1725705078: 2024-09-07 36 10:31:18: -1 month
1696674678: 2023-10-07 40 10:31:18: -1 year
1693387817: 2023-08-30 35 09:30:17: -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";
}

?>
1728297078: 2024-10-07 41 10:31:18: now
1728297079: 2024-10-07 41 10:31:19: 1 second
1728297138: 2024-10-07 41 10:32:18: 1 minute
1728300678: 2024-10-07 41 11:31:18: 1 hour
1728383478: 2024-10-08 41 10:31:18: 1 day
1728901878: 2024-10-14 42 10:31:18: 1 week
1730975478: 2024-11-07 45 10:31:18: 1 month
1759833078: 2025-10-07 41 10:31:18: 1 year
1763206339: 2025-11-15 46 11:32:19: 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";
}

?>
1728297079: 2024-10-07 41 10:31:19: now
1728172800: 2024-10-06 40 00:00:00: yesterday
1728259200: 2024-10-07 41 00:00:00: today
1728259200: 2024-10-07 41 00:00:00: midnight
1728302400: 2024-10-07 41 12:00:00: noon
1728345600: 2024-10-08 41 00:00:00: tomorrow
1728301500: 2024-10-07 41 11:45:00: front of 12
1728303300: 2024-10-07 41 12:15:00: back of 12
1728210679: 2024-10-06 40 10:31:19: previous day
1728383479: 2024-10-08 41 10:31:19: next day
1728210679: 2024-10-06 40 10:31:19: 1 day ago
1728383479: 2024-10-08 41 10:31:19: -1 day ago
1727778679: 2024-10-01 40 10:31:19: first day of
1730370679: 2024-10-31 44 10:31:19: 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 Tuesday the 8th of October 2024 at 10:31:19 AM
HomeMenu