PHP的日期时间运算总结
//GB2312的Encode
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
/*重点了解strtotime()函数
1、strftime比time()好用,可以直接把常用的’2010-02-03‘转成时间戳。
2、date()可以显示1970年前的时间。而不必用负数做参数2
3、日期计算可以用时间戳来中转。计算两个日期相差的天数,可以取得相差的时间戳后除以“24小时*60分*60”秒来得到,但用strtotime()更简洁、
4、了解用PEAR创建日历。这里略去。
知识点:网络上有关于date(Y-m-d,-800)来计算1970年前的时间,但WINDOW系统不支持负值,因此总会返回1970-1-1子夜。
*/
#PHP5必须先设置默认区。
date_default_timezone_set(ETC/GMT-8);
$nowdate=2010-02-23;
$lassdate = 2010-02-22;
echo strftime()函数输出的.strftime(%Y-%m-%d %H:%M:%S,time()).
;
echo date()函数输出的.date(Y-m-d H:i:s,time()).
;
//检查日期:boolean checkdate(int month,int day,int year)
$d=2010-2-31;
echo $d.是.(checkdate(2,31,2010)?有效日期!:无效日期!).
;
//确定当月天数
echo 本月有.date(t,time()).天
; //28天
//确定任意给定的月份的天数
$d=2008-02-01; //闰年,或$d=2008-02;不需要输入天也可以
$d=strtotime($d);
echo 2008年2月有.date(t,$d).天
; //29天
$d=getdate();
echo
;<br> print_r($d);<br> echo
/*Array(
[seconds] => 42
[minutes] => 16
[hours] => 13
[mday] => 23
[wday] => 2
[mon] => 2
[year] => 2010
[yday] => 53
[weekday] => Tuesday
[month] => February
[0] => 1266902202
)
*/
//echo date("Y-m-d H:i:s",-8000);
//setlocale(LC_ALL,zh_CN.gb2312); //setlocale函数对下面的没有影响。
#测试strftime,mktime函数。
echo strftime(今天是:%Y-%m-%d %H:%M:%S).
;
echo strtotime(now).
; // 等于time(),但strtotime使用范围更灵活,参下文.
echo 测试还原昨天时间:.date(Y-m-d,strtotime($lassdate)).
; //可以把字串型日期转成时间戳再用date转回原格式。
$x=strtotime($lassdate);
$y=mktime(0,0,0,2,22,2010);
echo strtotime()得到的昨天的时间戳是:.$x.,mktime()得到的昨天时间戳是:.$y.(($x==$y)?,二者相等:,二者不相同).
; //相等。
#显示1970年前的日期
$time_int=strtotime(1929-2-10);
echo date("Y-m-d ",$time_int).
; //在MYSQL中与date()函数相同功能的是date_format(1996-02-05 11:07:45,%Y-%m-%d)或for_format()
/*时间运算:
*请使用方法三。其它方法只供参考。 *
*/
#1、今天是23号,获得前天的时间,即减两天。
$predate=2;
$pretime=$predate*24*60*60; //2天的时间戳。
echo date(前天是:Y-m-d,time()-$pretime).
; //前天是:2010-02-21
#2、两个日期相差的天数。
$olddate = 2010-02-11; //如果要用mktime函数,则要用explode拆解日期。
$oldtime = strtotime($olddate);
$passtime = time()-$oldtime; //经过的时间戳。
echo 你在网上泡了.floor($passtime/(24*60*60)).天了.
; //12天。
#3、去年这个时侯。使用时要考虑闰年:平年365天,闰年366天。
#方法一:用减去全年天数的时间戳来获取。
$yDate=1;
$yDate_Y=date(Y,time())-1; //年份-1,即去年
$yDateYMD="$yDate_Y-01-01";
$yYMD=strtotime($yDateYMD); //去年的1月1号时间戳。
$d=date(L,$yYMD)?366:365; //是否是闰年
$yYearTime=$d*24*60*60;
$yYear=date(Y-m-d,time()-$yYearTime);
echo "去年的今天:$yYear
"; //2009-02-23
#方法二:用直接截取当前日期的年份减一,但不严谨,没有考虑到闰年。
#计算60年前的今天。忽略当中经过的闰年。
$yDate_Y=$yDate_Y-59;
$md=explode(-,date(Y-m-d));
$yYMD="$yDate_Y-{$md[1]}-{$md[2]}";
echo "60年前的今天:$yYMD
"; //1950-02-23
#方法三:用strtotime()和GNU日期语法---------推荐!
//3天后; //当前时间为2010-02-23
$d=strtotime(3 days);
echo 3天后.date(Y-m-d,$d)."
";
//3天前:
$d=strtotime(-3 days);
echo 3天前.date(Y-m-d,$d)."
"; //2010-02-20
//一个月前:
$d=strtotime(-1 months);
echo 一个月前.date(Y-m-d,$d)."
"; //2010-01-23
//2个月后:
$d=strtotime(2 months);
echo 二个月后.date(Y-m-d,$d)."
"; //2010-04-23
//1年前:
$d=strtotime(-1 years);
echo 1年前.date(Y-m-d,$d)."
"; //2009-02-23
//2小时前:
$d=strtotime(-2 hours);
echo 目前:.date(Y-m-d H:i:s,time()).,2小时前.date(Y-m-d H:i:s,$d)."
"; //目前:2010-02-23 13:38:49,2小时前2010-02-23 11:38:49
#DateTime构造函数:object DateTime([string $time [,dateTimeZone $timezone])
$date = new DateTime(2010-02-23 12:26:36);
echo $date->format(Y-m-d H:i:s)."
"; //和date()函数相同。2010-02-23 12:26:36
//重设时间:
//1、重设日期: boolean setDate(int year,int month,int day)
//2、重设时间: boolean setDate(int hour,int minute[,int second])
$date->setDate(2010,2,28);
echo $date->format(Y-m-d H:i:s)."
"; //2010-02-28 12:26:36
//日期计算,相当于上面的strtotime()
$date->modify("+7 hours");
echo $date->format(Y-m-d H:i:s)."
"; //2010-02-28 19:26:36
$date->modify("3 days");
echo $date->format(Y-m-d H:i:s)."
"; //2010-03-03 19:26:36 //从上面被改过的28号开始
/*PHP5在WIN不支持money_format函数?
setlocale(LC_MONETARY,zh_CN);
echo money_format("%i",786.56);//?Fatal error: Call to undefined function money_format()
*/
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
