AppDividend
Latest Code Tutorials

PHP Strtotime: Convert English Textual Datetime to TimeStamp

PHP supports date functions to handle date & time to implement and provide a method to convert human-readable dates to Unix Timestamp. One of those methods is strtotime(). Let’s see that in detail.

PHP strtotime()

The strtotime() is a built-in PHP String function used to convert an English textual date-time description to a UNIX timestamp. The strtotime() function accepts a string parameter in English, which represents the description of date-time.

 E.g., “now” refers to the current date in the English date-time description.

Syntax

strtotime(time, now);

Parameters

The time parameter specifies a date/time string.

The now parameter specifies the timestamp used to calculate the returned value. It is an optional parameter.

Since the time/date is not static, therefore the output will vary.

Example

See the following code.

<?php

echo strtotime("now"), "\n";
echo strtotime("10 September 1993"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+2 week"), "\n";
echo strtotime("+3 week 2 days 4 hours 11 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Sunday"), "\n";

See the following output.

➜  pro php app.php
1564733987
747619200
1564820387
1565943587
1566735598
1565222400
1564272000
➜  pro

Let’s get the current date.

<?php

echo date("Y-m-d", strtotime("now"));

See the following output.

➜  pro php app.php
2019-08-02                                                                      
➜  php

Let’s get the date from this year’s ending to the future one year.

<?php

echo date("d-m-Y", strtotime("2019-12-25 +1 year"));

See the following output.

➜  pro php app.php
25-12-2020                                                                      
➜  pro

Okay, now let’s use with str_replace() function.

<?php

$timestamp = strtotime(str_replace('/', '-', '19/02/2019'));
echo $timestamp;

See the output.

➜  pro php app.php
1550534400                                                                      
➜  pro

PHP strtotime timezone

Okay, let’s get the timestamp of the America/Los_Angeles timezone.

<?php

$dateStr = '2019-07-04';
$timezone = 'America/Los_Angeles';

$date = new DateTime($dateStr, new DateTimeZone($timezone));
$timestamp = $date->format('U');
echo $timestamp;

See the output.

➜  pro php app.php
1562223600                                                                      
➜  pro

Using strtotime to convert English text to a date

See the following code in which we are using the strtotime function to convert English text to data.

<?php

$textDate = "19th february 2004";

echo strtotime($textDate), "\n";

echo date("Y-m-d", strtotime($textDate))."\n";

See the following output.

➜  pro php app.php
1077148800
2004-02-19
➜  pro

So, we have converted the text to integer and the actual date.

Let’s print the next Sunday date from today’s date. Your output may be different as your current date will be different.

<?php

echo strtotime("next sunday"), "\n";  

echo date("Y-m-d", strtotime("next sunday"))."\n"; 

See the output.

➜  pro php app.php
1564876800
2019-08-04
➜  pro

Strtotime() doesn’t work with dd/mm/YYYY format

When you are working on the project, you often across a date format that looks like dd//mm/YYYY.

Suppose you have a date in dd/mm/YYYY format; how can you convert it to YYYY-mm-dd using the strtotime() function? But when you apply the strtotime() function to that format, then it won’t work. It works only with mm/dd/YYYY format.

Dates in the mm/dd/YYY or dd-mm-YYYY formats are illustrated by looking at the separator between the different components. If the separator is a slash (/), then the American mm/dd/YYYY is assumed. In contrast, if the separator is a dash (-) or a dot (.), then the European dd-mm-YYYY format is assumed.

See the following code.

// app.php

$date = '06/03/2021';
$date = str_replace('/', '-', $date);
echo date('Y-m-d', strtotime($date));

Output

2021-03-06

Convert strtotime to date time in PHP

 

Conclusion

The strtotime() function parses the English textual datetime into the Unix timestamp (the number of seconds since January 1, 1970, 00:00:00 GMT).

If the year is specified in the two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.

You need to careful of dates in the m/d/y or d-m-y formats; if the separator is the slash (/), then an American m/d/y is assumed.

If a separator is a dash (-) or a dot (.), then the European d-m-y format is expected. If we want to avoid potential errors, you should YYYY-MM-DD dates or date_create_from_format() when possible.

When using the “next month”, “last month”, “+1 month”,  “-1 month” or any combination of the +/-X months. It will give the non-intuitive results on Jan 30th and 31st.

Finally, the PHP strtotime() function example tutorial is over.

1 Comment
  1. Bob says

    Tell us what it returns if the date is INVALID!!!!

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.