How to increase time in php

藏色散人
Release: 2023-03-01 21:20:01
Original
5732 people have browsed it

How to increase time in php: first set the default time zone through "date_default_timezone_set"; then output tomorrow's time through "strtotime(' 1 day')"; finally modify the parameters to any number you want to increase.

How to increase time in php

php Add one day to a certain time? One hour? Time addition and subtraction

<?php
date_default_timezone_set(&#39;PRC&#39;); //默认时区
echo "今天:",date("Y-m-d",time()),"<br>";
echo "今天:",date("Y-m-d",strtotime("18 june 2008")),"<br>";
echo "昨天:",date("Y-m-d",strtotime("-1 day")),"<br>";
echo "明天:",date("Y-m-d",strtotime("+1 day")),"<br>";
echo "一周后:",date("Y-m-d",strtotime("+1 week")),"<br>";
echo "一周零两天四小时两秒后:",date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")), "<br>";
echo "下个星期四:",date("Y-m-d",strtotime("next Thursday")),"<br>";
echo "上个周一:".date("Y-m-d",strtotime("last Monday"))."<br>";
echo "一个月前:".date("Y-m-d",strtotime("last month"))."<br>";
echo "一个月后:".date("Y-m-d",strtotime("+1 month"))."<br>";
echo "十年后:".date("Y-m-d",strtotime("+10 year"))."<br>";
?>
Copy after login

//strtotime可以接受第二个参数,类型timestamp,为指定日期
echo date(&#39;Y-m-d&#39;, strtotime ("+1 day", strtotime(&#39;2011-11-01&#39;))),"\n";
Copy after login
<?php
echo "今天:",date(&#39;Y-m-d H:i:s&#39;),"<br>";//输出当前时间
echo "明天:",date(&#39;Y-m-d H:i:s&#39;,strtotime(&#39;+1 day&#39;));//输出明天时间
//这里+1 day 可以修改参数1为任何想需要的数  day也可以改成year(年),month(月),hour(小时),minute(分),second(秒)
//如:
date(&#39;Y-m-d H:i:s&#39;,strtotime("+1 day +1 hour +1 minute");
?>
Copy after login

Note: This method was tried after 1970, which is the scope of application of timestamps.

<?php
//下面这些代码是一些常用的日期处理函数了,可以两个时间的日期加减,两日期之差,日期转换时间截等。
echo date(&#39;Y-m-d&#39;,strtotime(&#39;+1 d&#39;,strtotime(&#39;2009-07-08&#39;)));//日期天数相加函数
echo date("Y-m-d",&#39;1246982400&#39;);
echo &#39;<br>&#39;;
echo date("Y-m-d",&#39;1279123200&#39;);
die();
$d   =   "2009-07-08 10:19:00";
echo   date("Y-m-d",strtotime("$d   +1   day"));   //日期天数相加函数
function dateToTime($d){//把日期转换成时间堆截
    $year=((int)substr("$d",0,4));//取得年份
    $month=((int)substr("$d",5,2));//取得月份
    $day=((int)substr("$d",8,2));//取得几号
    return mktime(0,0,0,$month,$day,$year);
    }
$Date_1="2009-07-08";
echo $Date_1+1;
$Date_2="2009-06-08";
$Date_List_a1=explode("-",$Date_1);
$Date_List_a2=explode("-",$Date_2);
$d1=mktime(0,0,0,$Date_List_a1[1],$Date_List_a1[2],$Date_List_a1[0]);
$d2=mktime(0,0,0,$Date_List_a2[1],$Date_List_a2[2],$Date_List_a2[0]);
$Days=round(($d1-$d2)/3600/24);
echo "两日期之前相差有$Days 天";
?>
Copy after login

Recommended: "PHP Tutorial"

The above is the detailed content of How to increase time in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!