Convert a Date to Timestamp in PHP

In this tutorial we will see How To Convert a Date to Timestamp in PHP. PHP strtotime() function can be used to convert date to timestamp, it parses an English textual datetime into a Unix timestamp.

PHP strtotime() function

PHP strtotime() function is used to parse English textual datetime into a Unix timestamp.

Unix timestamp is numbers of seconds from January 1 1970 00:00:00 GMT to the defined date. The Unix timestamp returned by the strtotime() doesn't provide information about the time zone.

See the examples given below which show how to use strtotime() function to convert date to timestamp.

In first example date given in Y-M-D format is converted into a timestamp.

<?php
$date = "2021-11-10";
$timestamp = strtotime($date);
echo $timestamp;
?>

Output

1636502400

These are number of seconds from January 1 1970 to November 10 2021.

In second example a date in different format is converted to timestamp.

<?php
$date = "November 30 2021";
$timestamp = strtotime($date);
echo $timestamp;
?>

Output

1638230400

In third example a date in American month, day and year format (12/22/78) is converted to timestamp.

<?php
$date = "11/25/21";
$timestamp = strtotime($date);
echo $timestamp;
?>

Output

1637798400

In fourth example date in (last day) format is converted to timestamp.

<?php
$date = "last Sunday";
$timestamp = strtotime($date);
echo $timestamp;
?>

Output

1636243200

This is how you can convert any date into timestamp, Only use allowed date formats with strtotime() function.

How To Append a String in PHP How To Prepend a String in PHP Replace a Word In String in PHP Convert Special Characters To HTML Entities PHP Find Number of Words in a String in PHP How To Find String Length in PHP How To Convert String To Uppercase in PHP How To Convert String To Lowercase in PHP How to Combine Two Strings in PHP Delete Array Elements By Value in PHP Get First Element Of Array In PHP Remove Numbers From String in PHP Convert Timestamp To Date in PHP Convert a Date to Timestamp in PHP Get Current Page Slug in PHP Check If Session Is Started in PHP Split Sentence Into Words in PHP Remove Last Character From String in PHP PHP Remove Spaces From String Replace Space With Dash in PHP Remove Everything from String Except Numbers Using PHP Get Full Url Address Using PHP Automatic Copyright Year Using PHP Generate 4 Digit Random Number in PHP How To Check If a Number is Prime using PHP Convert Integer to String in PHP