What we both know is that time & date is a rather useless distinction. In certain definitions of time, it also implies the date. So, from now on I want you to lớn think of time also containing dates. It is much more useful to lớn place these together and you probably will not give me a great reason khổng lồ separate the two after this tutorial. First, let’s get into explaining timestamps & some PHP date & time functions.
Bạn đang xem: Datetime
The PHP timestamp is a Unix timestamp that tracks the number of seconds since January 1, 1970 because we apparently believe milliseconds are useless. Nevertheless, let’s create a timestamp.Example
echo time();Result 1359780799So, we just use PHP’s time function with default parameters to lớn create a timestamp of this very moment (well, the moment I created this tutorial). Why would we want something lượt thích that when users cannot understand its format? First, it is a very consistent and easy to manipulate number. Second, I will show you how to lớn format the timestamp into PHP dates and times in just a second. Also, cảnh báo if you just want the current time, you should use time, but you should use mktime for custom datetimes.
Using our newly created timestamp, we can format it into a date that the user can understand.
Do you remember my tangent of times also include dates? Well, the guys that created PHP must have agreed with me. The PHP date function also can format timestamps into “Times”. Let’s see how to create a readable time.Example
echo date( "H:i:s","1359780799");Result 21:53:19Well done. Now, we have the current military time that I created this tutorial. I know you might be thinking, “How bởi vì I create a timestamp that is not the present?”. Relax, we will get to it in just a moment.
PHP Date and Time
Now, let’s set it up a little bit and create our own time and not the current time. We want to lớn format this into a full date và time string.Example
PHP Date and Time Formats
|a||‘am’ or ‘pm’ (lowercase)||pm|
|A||‘AM’ or ‘PM’ (uppercase)||PM|
|d||Day of month, a number with leading zeroes||20|
|D||Day of week (three letters)||Thu|
|h||Hour (12-hour format – leading zeroes)||12|
|H||Hour (24-hour format – leading zeroes)||22|
|g||Hour (12-hour format – no leading zeroes)||12|
|G||Hour (24-hour format – no leading zeroes)||22|
|i||Minutes ( 0 – 59 )||23|
|j||Day of the month (no leading zeroes||20|
|l (Lower ‘L’)||Day of the week||Thursday|
|L||Leap year (‘1’ for yes, ‘0’ for no)||1|
|m||Month of year (number – leading zeroes)||1|
|M||Month of year (three letters)||Jan|
|r||The RFC 2822 formatted date||Thu, 21 Dec 2000 16:01:07 +0200|
|n||Month of year (number – no leading zeroes)||2|
|s||Seconds of hour||20|
|y||Year (two digits)||06|
|Y||Year (four digits)||2006|
|z||Day of year (0 – 365)||206|
|Z||Offset in seconds from GMT||+5|