Home >Backend Development >PHP Tutorial >Summary of date and time operations in PHP_PHP tutorial
//Encode of GB2312
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
/*Focus on understanding the strtotime() function
1. strftime is easier to use than time(). It can directly convert the commonly used '2010-02-03' into a timestamp.
2. date() can display the time before 1970. Instead of using negative numbers as parameter 2
3. Date calculation can be transferred using timestamp. To calculate the number of days difference between two dates, you can get the difference timestamp and divide it by "24 hours * 60 minutes * 60" seconds, but it is more concise to use strtotime()
4. Learn how to create a calendar using PEAR. Omit it here.
Knowledge point: There is date(Y-m-d,-800) on the Internet to calculate the time before 1970, but the WINDOW system does not support negative values, so it will always return 1970-1-1 midnight.
*/
#PHP5 must set the default zone first.
date_default_timezone_set(ETC/GMT-8);
$nowdate=2010-02-23;
$lassdate = 2010-02-22;
The echo strftime() function outputs .strftime(%Y-%m-%d %H:%M:%S,time()).
;
The echo date() function outputs .date(Y-m-d H:i:s,time()).
;
//Check date: boolean checkdate(int month,int day,int year)
$d=2010-2-31;
echo $d.Yes.(checkdate(2,31,2010)? Valid date!: Invalid date!).
;
//Determine the number of days in the month
echo There are .date(t,time()).days this month
; //28 days
//Determine the number of days in any given month
$d=2008-02-01; //Leap year, or $d=2008-02; You don’t need to enter the day
$d=strtotime($d);
echo There are .date(t,$d).days in February 2008
; //29 days
$d=getdate();
echo
;<br> print_r($d);<br> echo;
//echo date("Y-m-d H:i:s",-8000);
//setlocale(LC_ALL,zh_CN.gb2312); //The setlocale function has no effect on the following.
#Test strftime, mktime functions.
echo strftime(Today is: %Y-%m-%d %H:%M:%S).
;
echo strtotime(now).
; // Equivalent to time(), but the usage range of strtotime is more flexible, see below.
echo test to restore yesterday's time:.date(Y-m-d,strtotime($lassdate)).
; //You can convert the string date into a timestamp and then use date to convert it back to the original format.
$x=strtotime($lassdate);
$y=mktime(0,0,0,2,22,2010);
Yesterday's timestamp obtained by echo strtotime() is: .$x., yesterday's timestamp obtained by mktime() is: .$y.(($x==$y)?, the two are equal:, the two are not Same).
; //Equal.
#Show dates before 1970
$time_int=strtotime(1929-2-10);
echo date("Y-m-d ",$time_int).
; //The same function as the date() function in MYSQL is date_format(1996-02-05 11:07:45,%Y-%m -%d) or for_format()
/*Time operation:
*Please use method three. Other methods are for reference only. *
*/
#1. Today is the 23rd. Get the time of the day before yesterday, that is, minus two days.
$predate=2;
$pretime=$predate*24*60*60; //2-day timestamp.
echo date(The day before yesterday was: Y-m-d, time()-$pretime).
; //The day before yesterday was: 2010-02-21
#2, The number of days difference between two dates.
$olddate = 2010-02-11; //If you want to use the mktime function, you need to use explode to disassemble the date.
$oldtime = strtotime($olddate);
$passtime = time()-$oldtime; //Elapsed timestamp.
echo You've been online.floor($passtime/(24*60*60)) for days.
; //12 days.
#3. This time last year. Leap years should be taken into account when using: 365 days in an ordinary year and 366 days in a leap year.
#Method 1: Get the timestamp minus the number of days in the year.
$yDate=1;
$yDate_Y=date(Y,time())-1; //Year-1, that is, last year
$yDateYMD="$yDate_Y-01-01";
$yYMD=strtotime($yDateYMD); //The timestamp of January 1 last year.
$d=date(L,$yYMD)?366:365; //Is it a leap year
$yYearTime=$d*24*60*60;
$yYear=date(Y-m-d,time()-$yYearTime);
echo "Today last year: $yYear
"; //2009-02-23
#Method 2: Use the year that directly intercepts the current date and subtract one, but it is not rigorous and does not take leap years into account.
#Calculate 60 years ago today. Ignore any leap years that pass.
$yDate_Y=$yDate_Y-59;
$md=explode(-,date(Y-m-d));
$yYMD="$yDate_Y-{$md[1]}-{$md[2]}";
echo "60 years ago today: $yYMD
"; //1950-02-23
#Method 3: Use strtotime() and GNU date syntax---------Recommended!
//3 days later; //The current time is 2010-02-23
$d=strtotime(3 days);
echo 3 days later.date(Y-m-d,$d)."
";
//3 days ago:
$d=strtotime(-3 days);
echo 3 days ago.date(Y-m-d,$d)."
"; //2010-02-20
//One month ago:
$d=strtotime(-1 months);
echo one month ago.date(Y-m-d,$d)."
"; //2010-01-23
//2 months later:
$d=strtotime(2 months);
echo two months later.date(Y-m-d,$d)."
"; //2010-04-23
//1 year ago:
$d=strtotime(-1 years);
echo 1 year ago.date(Y-m-d,$d)."
"; //2009-02-23
//2 hours ago:
$d=strtotime(-2 hours);
echo Current: .date(Y-m-d H:i:s,time())., 2 hours ago.date(Y-m-d H:i:s,$d)."
"; //Currently: 2010 -02-23 13:38:49, 2 hours ago 2010-02-23 11:38:49
#DateTime constructor: object DateTime([string $time [,dateTimeZone $timezone])
$date = new DateTime(2010-02-23 12:26:36);
echo $date->format(Y-m-d H:i:s)."
"; //Same as date() function. 2010-02-23 12:26:36
//Reset time:
//1. Reset date: boolean setDate(int year,int month,int day)
//2. Reset time: 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
//Date calculation, equivalent to strtotime()
above
$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 //Start from the 28th that was changed above
/*PHP5 does not support the money_format function in WIN?
setlocale(LC_MONETARY,zh_CN);
echo money_format("%i",786.56);//?Fatal error: Call to undefined function money_format()
*/
?>