Home > Backend Development > PHP Tutorial > php strtotime() function time and date conversion_PHP tutorial

php strtotime() function time and date conversion_PHP tutorial

WBOY
Release: 2016-07-20 11:03:06
Original
1026 people have browsed it

The strtotime() function in PHP parses the date and time description of any English text into a Unix timestamp. This function is also something we often use. Friends in need can refer to it. ​

Grammar
strtotime(time,now) parameter description
time specifies the time string to be parsed.
now is the timestamp used to calculate the return value. If this parameter is omitted, the current time is used.


Example

The code is as follows Copy code
 代码如下 复制代码

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";
?>

echo(strtotime("+5 hours"));

echo(strtotime("+1 week"));
echo(strtotime("+1 week 3 days 7 hours 5 seconds"));

echo(strtotime("next Monday"));
 代码如下 复制代码

$str = 'Not Good';

// 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('l dS of F Y h:i:s A', $timestamp);
}
?>

I just found out PHP thinks slashes in date-formats aren't very european:
(I guess this is not a bug, just the way it works. But correct me if I'm wrong.)

    $date = "06/10/2011 14:28"; // 6 october 2011 2:28 pm
    $otherDate = "06-10-2011 14:28"; // 6 october 2011 2:28 pm
   
    echo $stamp = strtotime($date) . "
"; // outputs 1307708880
    echo $otherStamp = strtotime($otherDate) . "
"; // outputs 1317904080
   
    echo date("d-m", $stamp); // outputs 10-06
    echo date("d-m", $otherStamp); // outputs 06-10

?>

echo(strtotime("last Sunday"));
?>Output:

1138614504

1128290400 1139219304 1139503709 1139180400 1138489200
Example #1 strtotime() example
The code is as follows Copy code
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"; ?>
Example #2 Failure check
The code is as follows Copy code
$str = 'Not Good'; // 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('l dS of F Y h:i:s A', $timestamp); } ?> I just found out PHP thinks slashes in date-formats aren't very european: (I guess this is not a bug, just the way it works. But correct me if I'm wrong.) $date = "06/10/2011 14:28"; // 6 October 2011 2:28 pm $otherDate = "06-10-2011 14:28"; // 6 October 2011 2:28 pm   Echo $stamp = strtotime($date) . " "; // outputs 1307708880 echo $otherStamp = strtotime($otherDate) . " "; // outputs 1317904080   echo date("d-m", $stamp); // outputs 10-06 echo date("d-m", $otherStamp); // outputs 06-10 ?>
http://www.bkjia.com/PHPjc/445317.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445317.htmlTechArticleThe strtotime() function in php parses the date and time description of any English text into a Unix timestamp. This function also We often use it, friends in need can refer to it. Grammar...
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