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

How to use PHP time conversion function?

Guanhui
Release: 2023-03-01 16:50:02
Original
2572 people have browsed it

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";
?>
Copy after login

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);
}
?>
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template