Home  >  Article  >  Backend Development  >  How to get the time in a few days in php

How to get the time in a few days in php

藏色散人
藏色散人Original
2021-06-01 09:24:544727browse

php method to get the time in a few days: first create a PHP sample file; then use "date("Y-m-d H:i:s",strtotime("3 day"));" to get the time in 3 days Time is enough.

How to get the time in a few days in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

php method to get the time and date after N days

Example 1: php gets the time and date after 3 days

Code:

<?php
//当前时间
    echo date(&#39;Y-m-d H:i:s&#39;);
    //3天之后的时期
    echo date("Y-m-d H:i:s",strtotime("3 day"));
?>

Output result:

2019-11-07 23:23:25
2019-11-10 23:23:25

Example 2: php gets the time after n days Date function

Function code:

<?php
function get_later($d){
    return date("Y-m-d H:i:s",strtotime($d." day"));
}
?>

Function call:

<?php
echo get_later(7);
?>

Supplementary instructions:

Flexibly use PHP’s strtotime(), you can also get The time and date are N hours before, N hours after, one week ago, one month after, etc. of the current time.

Example:

<?php
//一小时之前的时间
echo strtotime("-1 hours");
//五小时之后的时间
echo strtotime("+5 hours");
//一周之后的时间 
echo strtotime("+1 week");
//下周一的时间
echo strtotime("next Monday");
?>

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to get the time in a few days in php. 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