The date and Strtotime functions in PHP are magical

Source: Internet
Author: User
Tags date time stamp iso 8601 iso 8601 format local time

PHP two commonly used date related functions date and strtotime, I believe we must not be unfamiliar. But we usually use all just basic function, what time stamp change date format, date format change timestamp.

In fact, these two functions have a deeper use:

1. Date function Date (format,timestamp) format, timestamp as timestamp (do not fill in the default current timestamp)

There are many options for format:
(1) Day
The day of the D Month, with a leading zero 2-digit number 01 to 31 for example: Date (' d ') output:  
The day of the J Month, without leading 1 to 31 for example: Date (' J ') output: 7  
  Day 0 to 365 in the z -year
(2) Week
The day of the D -week, the text says, 3 letters Mon to Sun  
  l(lowercase letter of "L") Day of the week, full text format Sunday to Saturday
  N ISO-8601 format number represents the day of the week (PHP 5.1.0 new Plus) 1 (for Monday) to 7 (represents Sunday)
  S English suffix, 2 characters st,nd,rd or th, after the number of days per month. Can be used with J.
  The day of the W Week, the number represents 0 (representing Sunday) to 6 (for Saturday)
  The week of the year in W ISO-8601 format, starting from Monday (PHP 4.1.0 New) For example: 42 (42nd Week of the year)
(3) months
F Month, full text format, such as January or March January to December  
  N number represents the month, without leading 1 to
the month represented by the m number, with a leading 1 to  
  M Three-letter abbreviation for month Jan to Dec
  T the month specified has a few days 28 to 31
(4)years
   Lis a leap year if the leap year is 1, otherwise 0
o ISO-8601 format year number. This is the same value as Y, except if the day of the Week (W) of the ISO belongs to the previous year or the next year. (PHP 5.1.0 New) examples:1999 or 2003 
  Y 4-digit year full representation for example: 1999 or 2003
  y 2 digits indicate the year for example: 99 or 03
(5)Time
   a lowercase morning and afternoon value am or PM
A uppercase morning and afternoon value am or PM  
B Swatch Internet Standard 000 to 999  
G -hour, 12-hour format, no leading 1 to  
   GHours, 24-hour format, no leading 00 to 23
   hHours, 12-hour format, with leading 1 to 12
   HHours, 24-hour format, with leading 000 to 23
   INumber of minutes with leading zeros 00 to 59
   sNumber of seconds with leading 000 to 59
   uMilliseconds (PHP 5.2.2 new Plus). It is important to note that the date () function always returns 000000 because it accepts only the integer parameter, and Datetime::format () supports milliseconds. Example: 654321
(6) Time zone
   eTime zone ID (PHP 5.1.0 New) Example: Utc,gmt,atlantic/azores
   Iis daylight saving time if daylight saving time is 1, otherwise 0
   OHours of difference from GMT for example: +0200
   PUnlike Greenwich mean (GMT), the hours and minutes are separated by colons (PHP 5.1.3) For example: +02:00
   TThe local time zone for example: EST,MDT ("translator note" is in full text format under Windows, such as "Eastern Standard Time", the Chinese version will show "Chinese standards").
   ZThe number of seconds of the slack offset. The time zone offset in the west of UTC is always negative, and the time zone offset to the east of UTC is always positive. 43200 to 43200
(7) Full Date/time
   CDate in ISO 8601 format (PHP 5 new Plus) 2004-02-12t15:19:21+00:00
   RDate in RFC 822 format example: Thu, DEC 2000 16:01:07 +0200
   UThe number of seconds since the Unix era (January 1 1970 00:00:00 GMT) has been the same as time ()
2. Strtotime function
Strtotime (time,now);
Time is a specified date string, typically Yyyy-mm-dd h:i:s 2018-05-01 07:50:10
Now is a timestamp that specifies the timestamp used to calculate the return value. If this argument is omitted, the current time is used.
Echo(Strtotime("Now"). " <br> ");//Returns the current timestampEcho(Strtotime("October 1980"). "<br>");//returns the specified date time stampEcho(Strtotime("+5 hours"). "<br>"); Returns a timestamp of 5 childEcho(Strtotime("+1 Week"). "<br>");//Return to the present after a weekEcho(Strtotime("+1 Week 3 days 7 hours 5 Seconds"). "<br>");//Returns the timestamp after a week, plus three days, 7 hours, 5 seconds .Echo(Strtotime("Next Monday"). "<br>");//Return the next one-week timestampEcho(Strtotime("Last Sunday"); /Return last Sunday time stamp

And the same is true

Strtotime ("-1 day");//The present time stamp of yesterday

Strtotime ("+1 Day");//The present time stamp of tomorrow

Pay special attention to get last month's today, last day of last month, first day of last month

    Echo Date(' y-m-d ',Strtotime(Date(' y-m-01 '). '-1 month '));//calculate the first day of this month minus another one months .    Echo Date(' y-m-d ',Strtotime(Date(' y-m-01 '). '-1 day '));//calculate the first day of the month minus another day//get the first day of the month and the last day.   Echo Date(' y-m-01 ',Strtotime(' 0 Day ')); Echo Date(' Y-m-t ',Strtotime(' 0 Day '));//gets the day of the year, month, day, and number of days.   Echo"Total this month:".Date("T"). " Days; Echo"Current year".Date(' Y '); Echo"Current Month".Date(' m '); Echo"What's the current date".Date(' d '); /** * Calculated for one months today, if not today for the last month, then return last day of one months * @param type $time * @return type*/$time=Strtotime("2018-07-31");functionLast_month_today ($time){    $last _month_time=Mktime(Date("G",$time),Date("I",$time),Date("s",$time),Date("N",$time), 0,Date("Y",$time)); $last _month_t=Date("T",$last _month_time); if($last _month_t<Date("J",$time)) {        return Date("Y-m-t h:i:s",$last _month_time); }    return Date(Date("Y-m",$last _month_time) . "-D",$time);}EchoLast_month_today ($time);

Mktime Use Format:

Mktime (HOUR,MINUTE,SECOND,MONTH,DAY,YEAR,IS_DST); Returns the timestamp of the date (not the GMT date, the time zone configured according to the profile)

Gmmktime (HOUR,MINUTE,SECOND,MONTH,DAY,YEAR,IS_DST); Returns the timestamp of the date (GMT date, Beijing time should be reduced by 8 hours)

  
  


The date and Strtotime functions in PHP are magical

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.