Home >Backend Development >PHP Problem >How to use PHP time conversion function?

How to use PHP time conversion function?

Guanhui
GuanhuiOriginal
2020-06-23 17:46:142571browse

How to use PHP time conversion function?

PHP time conversion function

The time conversion function in PHP is "strtotime()". The function of this function is to convert any English text The date or time description is parsed into a Unix timestamp, and its syntax is "strtotime(time,now)". If the function succeeds, it returns the timestamp, otherwise it returns FALSE.

Simple example

<?php
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
?>

Failure check

<?php
$str = &#39;Not Good&#39;;
// previous to PHP 5.1.0 you would compare with -1, instead of false
if (($timestamp = strtotime($str)) === false) {
    echo "The string ($str) is bogus";
} else {
    echo "$str == " . date(&#39;l dS of F Y h:i:s A&#39;, $timestamp);
}
?>

Recommended tutorial: "PHP Tutorial

The above is the detailed content of How to use PHP time conversion function?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn