PHP time-of-Day Operations Summary _php Tutorial

Source: Internet
Author: User
Tags echo date
GB2312 's Encode
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 than time () easy to use, you can directly use the ' 2010-02-03 ' to turn into timestamps.
2. Date () can show the time 1970 years ago. Instead of using negative numbers for parameter 2.
3, date calculation can be used to relay time stamp. Calculates the number of days that differ by two dates, can be obtained by dividing the time stamp by "24 hours *60 minutes *60" seconds, but with Strtotime () more concise,
4, learn to create a calendar with pear. omitted here.
Knowledge Point: The network has about date (y-m-d,-800) to calculate the time 1970 years ago, but the window system does not support negative values, so always return to the 1970-1-1 midnight.
*/

#PHP5必须先设置默认区.
Date_default_timezone_set (etc/gmt-8);
$nowdate =2010-02-23;
$lassdate = 2010-02-22;

The echo strftime () function outputs the. strftime (%y-%m-%d%h:%m:%s,time ()).
;
The. Date (y-m-d h:i:s,time ()) output of the Echo date () function.
;
Check Date: boolean checkdate (int month,int day,int year)
$d =2010-2-31;
echo $d. Yes. (Checkdate (2,31,2010)? Effective Date! : Invalid date! ).
;


Determine the number of days in the month
Echo has. Date (T,time ()) this month. Day
; 28 Days
Determine the number of days for any given month
$d =2008-02-01; Leap year, or $d=2008-02; no need to enter days
$d =strtotime ($d);
Echo February 2008 has. Date (T, $d). Day
; 29 days

$d =getdate ();
Echo

;
Print_r ($d);
;
/*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); The setlocale function has no effect on the following.
#测试strftime, Mktime function.
Echo Strftime (today is:%y-%m-%d%h:%m:%s).
;
Echo Strtotime (now).
; Equals time (), but the strtotime uses a more flexible, reference context.
Echo Test restores yesterday's time:. Date (Y-m-d,strtotime ($lassdate)).
; You can turn a string date into a timestamp and return to the original format with date.
$x =strtotime ($lassdate);
$y =mktime (0,0,0,2,22,2010);
Echo strtotime () The timestamp of yesterday was:. $x., mktime () The timestamp of yesterday was:. $y. (($x = = $y)?, both equal:, the two are different).
; Equal.

#显示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 number 23rd, get the day before yesterday, that is, two days minus.
$predate = 2;
$pretime = $predate *24*60*60; 2-day timestamp.
echo Date (the day before yesterday: Y-m-d,time ()-$pretime).
; The day before yesterday was: 2010-02-21

#2, the number of days between two dates.
$olddate = 2010-02-11; If you want to use the Mktime function, use explode to disassemble the date.
$oldtime = Strtotime ($olddate);
$passtime = Time ()-$oldtime; The elapsed time stamp.
Echo, you're on the Internet. Floor ($passtime/(24*60*60)). Oh, my God.
; 12 days.

#3, this time last year. Consider leap years when using: Common year 365 days, leap year 366 days.
#方法一: Obtained by subtracting the number of days of the year from the timestamp.
$yDate = 1;
$yDate _y=date (Y,time ())-1; Year-1, last year
$yDateYMD = "$yDate _y-01-01";
$yYMD =strtotime ($yDateYMD); Time stamp of last year's January 1.
$d =date (L, $yYMD)? 366:365; Whether it is a leap year
$yYearTime = $d *24*60*60;

$yYear =date (Y-m-d,time ()-$yYearTime);
echo "Last year's Today: $yYear
"; 2009-02-23
#方法二: The year of the current date is truncated by one, but not strictly, without regard to leap years.
#计算60年前的今天. Ignores the leap years that pass through.
$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

#方法三: Recommended with Strtotime () and GNU date syntax---------!
3 days later; 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 months ago:
$d =strtotime ( -1 months);
echo one months ago. Date (y-m-d, $d). "
"; 2010-01-23

After 2 months:
$d =strtotime (2 months);
echo two months later. Date (y-m-d, $d). "
"; 2010-04-23

1 years ago:
$d =strtotime ( -1 years);
echo 1 years ago. Date (y-m-d, $d). "
"; 2009-02-23

2 hours ago:
$d =strtotime ( -2 hours);
Echo currently:. Date (y-m-d h:i:s,time ())., 2 hours ago. Date (y-m-d h:i:s, $d). "
"; Current: 2010-02-23 13:38:49, 2 hours ago 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). "
"; Same as the 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 the above 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//start with the changed number 28th above

/*PHP5 does not support Money_format function in win?
SetLocale (LC_MONETARY,ZH_CN);
Echo Money_format ("%i", 786.56);//? Fatal error:call to undefined function money_format ()
*/
?>

http://www.bkjia.com/PHPjc/508431.html www.bkjia.com true http://www.bkjia.com/PHPjc/508431.html techarticle php//gb2312 encode header (Cache-control:no-store, No-cache, must-revalidate); Header (Cache-control:post-check =0, pre-check=0, false); /* Focus on understanding Strtotime () function 1, strf ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.