PHP strtotime() Function

PHP strtotime() Function is used to convert an English textual datetime into a Unix timestamp. A strtotime() function is PHP built-in function.

PHP Version : PHP 4, PHP 5, PHP 7, PHP 8

Syntax for strtotime:

strtotime(string $datetime,  int|null $now= null);

Parameter,

$datetime : Required. It is a date/time string.
$now : Optional. The timestamp which is used as a base for the calculation of relative dates.

Return values for strtotime:

PHP strtotime() Function returns a timestamp on success otherwise it returns false on failure.

Errors/Exceptions:
It will generate a E_NOTICE if the time zone is not valid.

Let's see below example to understand php strtotime() Function in details.

 

Example :

<?php
echo "<br> Unix timestamp for current time : ".strtotime("now");
echo "<br> Unix timestamp : ".strtotime("+12 hours");
echo "<br> Unix timestamp : ".strtotime("+45 minutes");
echo "<br> Unix timestamp : ".strtotime("+3 week");
echo "<br> Unix timestamp : ".strtotime("last Sunday");
echo "<br> Unix timestamp : ".strtotime("+3 week 6 days 2 hours 7 seconds");
echo "<br> Unix timestamp : ".strtotime("next Tuesday");
echo "<br> Unix timestamp : ".strtotime("28 May 1967");
?>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

97877