php date

PHP Date and Time

PHP date() function is used to format date or time.

PHP Date() Function

The PHP Date() function formats a timestamp into a more readable date and time.

QQ截图20161008172535.png


Note: A timestamp is a sequence of characters that represents the date and event when a specific event occurred.

Get a simple date

The format parameters of the date() function are required and they specify how to format the date or time.

Here are some characters commonly used in dates:

·   d - represents the day of the month (01-31)

·         m - represents the month (01 -12)

· Y - represents the year (four digits)

·   1 - represents a day of the week

Other characters, such as "/", ". " or "-" can also be inserted into characters to add additional formatting.

The following examples format today's date in three different ways:

Example

<?php
echo "今天是 " . date("Y/m/d") . "<br>";
echo "今天是 " . date("Y.m.d") . "<br>";
echo "今天是 " . date("Y-m-d") . "<br>";
echo "今天是 " . date("l");
?>

PHP Tip - Automatic Copyright Year

Use the date() function to automatically update the version year on your website:

Example

© 2010-<?php echo date("Y")?>

Get simple time

The following characters are commonly used for time:

##·  h - 12-hour hour format with leading zeros

·  i - With the first zero minutes

# · S -with the first zero second (00 -59)

· A -

The following example outputs the current time in the specified format:

Example

<?php
echo "现在时间是 " . date("h:i:sa");
?>

Note: Please note that the PHP date() function will return the current date/time of the server!

Getting Time Zone

If the time returned from the code is not the correct time, it is possible that your server is located in another country or is set to a different time zone.

So if you need an accurate time based on a specific 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:

Example

<?php
date_default_timezone_set("Asia/Shanghai");
echo "当前时间是 " . date("h:i:sa");
?>

Create date through PHP mktime()

The optional timestamp parameter in the date() function specifies the timestamp. If you do not specify a timestamp, the current date and time will be used (as in the example above).

The mktime() function returns the Unix timestamp of a date. A Unix timestamp contains the number of seconds between the Unix epoch (January 1, 1970 00:00:00 GMT) and the specified time.

Syntax

mktime(hour,minute,second,month,day,year)

The following example uses a series of parameters in the mktime() function to Create date and time:

Example

<?php
$d=mktime(9, 12, 31, 6, 10, 2015);
echo "创建日期是 " . date("Y-m-d h:i:sa", $d);
?>

Create date with string through PHP strtotime()

PHP strtotime() function is used to convert human-readable string Convert to Unix time.

Syntax

strtotime(time,now)

The following example creates date and time through the strtotime() function:

Example

<?php
$d=strtotime("10:38pm April 15 2015");
echo "创建日期是 " . date("Y-m-d h:i:sa", $d);
?>

PHP is really smart about converting strings to dates, so you can use a variety of values:

Examples

<?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>";
?>

Strtotime() isn't perfect, though, so Remember to check the string you put in it.

More date examples

The following example outputs the date of next Saturday:

Example

<?php
$startdate = strtotime("Saturday");
$enddate = strtotime("+6 weeks",$startdate);
 
while ($startdate < $enddate) {
  echo date("M d", $startdate),"<br>";
  $startdate = strtotime("+1 week", $startdate);
}
?>

The following example outputs July Number of days before four days:

Example

<?php
$d1=strtotime("December 31");
$d2=ceil(($d1-time())/60/60/24);
echo "距离十二月三十一日还有:" . $d2 ." 天。";
?>


Continuing Learning
||
<?php date_default_timezone_set("Asia/Shanghai"); echo "当前时间是 " . date("h:i:sa"); ?>
submitReset Code