2016年2月14日 星期日

php Advanced : Date and time

PHP 5 Date and Time


Get a Simple Date

The required format parameter of the date() function specifies how to format the date (or time).
Here are some characters that are commonly used for dates:
  • d - Represents the day of the month (01 to 31)
  • m - Represents a month (01 to 12)
  • Y - Represents a year (in four digits)
  • l (lowercase 'L') - Represents the day of the week
Other characters, like"/", ".", or "-" can also be inserted between the characters to add additional formatting.
The example below formats today's date in three different ways:

<?php
echo "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");
?>

執行結果如下:    看起來"l"  會出現今天是禮拜幾....

Get Your Time Zone

If the time you got back from the code is not the right time, it's probably because your server is in another country or set up for a different timezone.
So, if you need the time to be correct according to a specific location, you can set a timezone to use.

The example below sets the timezone to "America/New_York", then outputs the current time in the specified format:

<?php
date_default_timezone_set("America/New_York");
echo "The time is " . date("h:i:sa");
?>

執行結果如下:





沒有留言:

張貼留言