PHP with handwritten notes built-in function date

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

1. Date_default_timezone_set

date_default_timezone_set- Setting the default time zone for all datetime functions in a script

When you use only the Mktime () function in PHP, you sometimes get an error, and the workaround is to add a line after <?php

Date_default_timezone_set (' Asia/shanghai ');

2. Date built-in function parameter list

date- Formatting a local time/date

Descriptionstring date ( string $format [, int $timestamp ])

Returns a string timestamp that is the result of an integer in a given format string. If no timestamp is given, the local current time is used. In other timestamp words, it is optional and the default value is Time ().

The following form is referenced from the official PHP manual and is available here for easy access:

D The day ordinal of a month with a leading zero 2-digit number to
D Day of the week, text representation, 3 letters Mon to Sun
J The day ordinal of the month without leading zeros 1 to
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(= Sunday)
S English suffix, 2 characters, after the number of days per month St,nd,Rd or th. Can be used with J .
W Day of the week, numbers indicate 0(for Sunday) to 6(= Saturday)
Z The day ordinal of the year 0 to 366
Week --- ---
W Week of the ISO-8601 format year, starting from Monday (PHP 4.1.0 New) For example: (42nd week of the year)
Month --- ---
F month, full text format, such as January or March January to December
M A number represents the month, with a leading zero to
M Three letter abbreviation for month Jan to Dec
N The number represents the month, without leading zeros 1 to
T The number of days that a given month should be from
Years --- ---
L Whether it is 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 addition) Examples: 1999 or 2003
Y 4-digit year in full representation Example:1999 or 2003
Y Year 2-digit representation For example :
Time --- ---
A Lowercase morning and afternoon values am or pm
A Uppercase morning and afternoon values AM or PM
B Swatch Internet Standard Time to 999
G Hours, 12-hour format, no leading zeros 1 to
G Hours, 24-hour format, no leading zeros 0 to
H Hours, 12-hour format, with leading zeros to
H Hours, 24-hour format, with leading zeros From xx to
I Number of minutes with leading zeros From xx to >
S Number of seconds with leading zeros From xx to >
Time --- ---
E Time zone ID (PHP 5.1.0 new addition) For example:UTC,GMT,atlantic/azores
I Whether it is daylight saving time If daylight saving time is 1, otherwise 0
O Hours of difference from GMT Example:+0200
P Difference from GMT (GMT), between hours and minutes colon separated (PHP 5.1.3 New) Example:+02:00
T The time zone in which this machine resides For example:EST,MDT(the "translator note" is in full text format under Windows, such as "Eastern Standard Time", Chinese version will show "China normal times").
Z The 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
Full Date/Time --- ---
C Date in ISO 8601 format (PHP 5 new) 2004-02-12t15:19:21+00:00
R Date in RFC 822 format For example:Thu, Dec 16:01:07 +0200
U The number of seconds since the Unix era (January 1 1970 00:00:00 GMT) started See Time ()

  

Eg:date () function reality a time

  

1 <? PHP 2 Echo Date ("Y-m-d h:i:s",Time()); 3 Echo "<br/>"; 4 Echo Date ("Y-m-d h:i:s",mktime(13,55,0,12,6,2017)); 5 Echo "<br/>";

The results are shown as:

  

Parameters can not be identified in the elements will be preserved, with JS need a large string of code, PHP line of code to achieve!!!

3. PHP implementation of two time difference and simple calendar display
1<meta charset= "UTF-8"/>2<?PHP3Date_default_timezone_set ("Asia/shanghai");4 Echo";5 6 functionGet_time_diff ($time 1=0,$time 2=0){7     if($time 1==0&&$time 2==0){8         $diff= Time();9}ElseIf($time 1>0&&$time 2==0){Ten         $diff=$time 1; One}Else{ A         $diff=ABS($time 1-$time 2); -     } -  the     $s=$diff%60; -     $i= Floor($diff%3600/60); -     $h= Floor($diff% (3600*24)/3600); -     $d= Floor($diff/(3600*24)); +  -     return"<p>".$d." Days. "$h." Hours. "$i." Minutes ".$s." Seconds </p> "; +  A } at Echo Date("Y-m-d h:i:s"); - EchoGet_time_diff (); - EchoGet_time_diff (Mktime(0,0,0,1,2,1970)); - EchoGet_time_diff (Mktime(0,0,0,12,8,2017),Mktime(0,0,0,12,5,2017)); -  - Echo"; in  - functionCalendar$time=0){ to     $time=$time>0?$time: Time(); +  -     $days=Date("T",$time);//total days of the month the  *     $first _day=Date("W",Mktime(0,0,0,Date("N",$time), 1,Date("Y",$time)));//Day 1th of the month is the day of the week $ Panax Notoginseng     $rows=Ceil(($days+$first _day)/7);//number of rows in the dynamic part of the calendar -  the     $count= 1; +  A     $current _day=Date("J",$time);//What day is the date? the  +     Echo"<table border= ' 1 ' >"; -     Echo"<tr><th colspan= ' 7 ' >".Date("Y year m Month",$time)." </th></tr> "; $     Echo"<tr><th> Day </th><th> one </th><th> two </th><th> three </th><th> four </th><th> Five </th><th> six </th></tr> "; $      for($i= 1;$i<=$rows;$i++){ -         Echo"<tr>"; -          for($j= 0;$j<7;$j++){ the  -             if( ($i==1&&$j<$first _day) || ($i==$rows&&$count>$days) ){Wuyi                 Echo"<td>&nbsp;</td>"; the}Else{ -                 if($count==$current _day){ Wu                     Echo"<td style= ' color:red; ' > ".$count." </td> "; -}Else{ About                     Echo"<td>".$count." </td> "; $                 } -                 $count++; -             } -  A         } +         Echo"</tr>"; the     } -     Echo"</table>"; $ } the calendar (); theCalendarMktime(0,0,0,10,1,2008));

The results of the operation are as follows:

PHP with handwritten notes built-in function date

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.