PHP Date and time
PHP Multidimensional Arrays
PHP Include
The PHP date () function is used to format dates or times.
PHP Date () function
The PHP date () function formats the timestamp as an easier-to-read date and time.
Grammar
Date (Format,timestamp)
Parameter description
Format required. Specifies the format of the timestamp.
Timestamp is optional. Time stamp is specified. The default is the current time and date.
Note: A timestamp is a sequence of characters that represents the date and event that a specific event occurred.
Get a simple date
The format parameters of the date () function are required, and they specify how the date or time is formatted.
Some of the characters commonly used in dates are listed below:
D-Represents the day of the month (01-31)
M-Denotes month (01-12)
Y-Represents the year (four digits)
1-Indicates the day of the week
Other characters, such as "/", "." or "-" can also be inserted into characters to add additional formatting.
The following example formats today's date in three different ways:
Instance
<?phpecho "Today is". Date ("Y/m/d"). "<br>"; echo "Today is." Date ("Y.M.D"). "<br>"; echo "Today is." Date ("y-m-d"). "<br>"; echo "Today is." Date ("L");? >
Running an instance
PHP Tips-Automatic copyright year
Use the date () function to automatically update the version year on your site:
Instance
2010-<?php echo Date ("Y")?>
Running an instance
Get a simple time
The following characters are commonly used for time:
H-12 hour format with first zero
I-minutes with first zero
S-Seconds with First zero (00-59)
A-lowercase noon and afternoon (AM or PM)
The following example outputs the current time in the specified format:
Instance
<?phpecho "Time is now". Date ("H:i:sa");? >
Running an instance
Note: Be aware that the PHP date () function returns the current date/time of the server!
Get time Zone
If the correct time is returned from the code, it is possible that your server is in another country or is set to a different time zone.
Therefore, if you need accurate time based on the exact location, you can set the time zone to use.
The following example sets the time zone to "Asia/shanghai" and then outputs the current time in the specified format:
Instance
<?phpdate_default_timezone_set ("Asia/shanghai"); echo "Current time is". Date ("H:i:sa");? >
Running an instance
Create a date with PHP mktime ()
The optional timestamp parameter in the date () function specifies the time stamp. If you do not specify a timestamp, the current date and time will be used (as in the previous example).
The Mktime () function returns the Unix timestamp of the date. The UNIX timestamp contains the number of seconds between the Unix era (January 1, 1970 00:00:00 GMT) and the specified time.
Grammar
Mktime (Hour,minute,second,month,day,year)
The following example uses a series of parameters in the Mktime () function to create a date and time:
Instance
<?php$d=mktime (9, A, 6, ten); echo "Creation date is". Date ("Y-m-d H:i:sa", $d);? >
Running an instance
Create a date with a string using PHP strtotime ()
The PHP strtotime () function is used to convert human-readable strings to Unix time.
Grammar
Strtotime (Time,now)
The following example creates a date and time by using the Strtotime () function:
Instance
<?php$d=strtotime ("10:38pm April"); echo "Creation date is". Date ("Y-m-d H:i:sa", $d);? >
Running an instance
PHP is smart enough to convert a string to a date, so you can use a variety of values:
Instance
<?php$d=strtotime ("Tomorrow"); Echo Date ("Y-m-d H:i:sa", $d). "<br>", $d =strtotime ("Next Saturday"), Echo date ("Y-m-d H:i:sa", $d). "<br>", $d =strtotime ("+3 Months"), Echo date ("Y-m-d H:i:sa", $d). "<br>";? >
Running an instance
However, Strtotime () is not perfect, so remember to check the string into which it is placed.
More Date Instances
The following example prints the date of the next Saturday:
Instance
<?php$startdate = Strtotime ("Saturday"), $enddate = Strtotime ("+6 weeks", $startdate); while ($startdate < $enddate { echo date ("M D", $startdate), "<br>"; $startdate = Strtotime ("+1 Week", $startdate);}? >
Running an instance
The following example outputs the number of days before July 4:
Instance
<?php$d1=strtotime ("December"), $d 2=ceil (($d 1-time ())/60/60/24), echo "from December 31 also:". $d 2. "Days. ";? >