Introduced
PHP is a very amazing language. It's strong enough (the core language of the biggest blog (WordPress)), it's wide enough (running on Facebook, the biggest social networking site), and it's simple enough (as a beginner's preferred starter language). Run well on Low-cost machines. and PHP language has a lot of very good server suite (such as Wamp and Mamp), very convenient to install on your machine. PHP has a very rich library resource, which makes it easy for developers to handle some business. Since we are in the project with the most contact date, so today we learn from the date function.
Give a simple date example
I will use the echo command to output the content to our client (browser). I will use the following code as the base code.
Copy Code code as follows:
<! DOCTYPE html>
<meta charset= "Utf-8"/>
<title>getting started with dates in php5</title>
<body>
<?php
Date_default_timezone_set (' Asia/shanghai ');
echo "Today is", date (' L ');
?>
</body>
You will see the following in your browser.
Copy Code code as follows:
This function outputs the text format of the day of the week. The date function requires at least one character argument (this argument tells us how to format the current date).
Try a different format
If you look at the PHP date function in the PHP manual, you will find that there are many ways to format dates.
Copy Code code as follows:
<?php echo "Today is", date (' y-m-d ');?>
Will get
Today is 2012-08-17
There are some dates that are commonly used, so PHP provides some constants for you to use. For example, you can use cookies to get to the client date.
Copy Code code as follows:
<?php echo "Today is", date (Date_cookie);?>
You will get as follows
Copy Code code as follows:
Today is Friday, 17-aug-12 11:34:38 CST
Note Do not use quotes when using constants.
What time is it now?
If you want to output the current time, you can use date (different format character parameters).
Copy Code code as follows:
<?php echo "The Time is", Date (' G:i:sa ');?>
You will get
The time is 11:39:59am
Localize your time zone
If you find that the above code doesn't give you the right time, it's probably because your server is setting up a different time zone from your local. You need to specify the time zone on the server, then you use the following code:
Copy Code code as follows:
<?php date_default_timezone_set (' Asia/shanghai ');?>
This will set China's Shanghai time zone. This is the PHP5 function (note the old version of PHP), and there are plenty of time zones for you to choose from. If you want to be permanently effective, you can modify your php.ini file.
Get another Time
You often need other time, not the present time. When you create time with the date () function, the system uses UNIX system time. This time represents the number of seconds from January 1, 1970 00:00:00 GMT (Unix era time) to now.
In order to specify how to get the date of the specified time, you can provide the second parameter of the date (0 function) as the number of seconds.
Copy Code code as follows:
<?php echo "Today is", date (' y-m-d ', 1309133434);?>
The result:
Today is 2011-06-27
This does not look useful, but it means that you can do the calculations with the date () function. Before that, you need to simply create a timestamp.
Create a time stamp
There are many ways to create timestamps. We can use the Mktime () function to get the time stamp we need.
Copy Code code as follows:
<?php
$mytime =mktime (9, 23, 33, 6, 26, 2011);
echo "Today is", date (' y-m-d g:i:sa ', $mytime);
?>
The results obtained are:
Today is 2011-06-26 9:23:33am
The Mktime () function requires you to pass the hours, minutes, seconds, months, days, and years in turn. It's a good way to get time stamps, but there's a cooler way.
Get time stamp by character
You can use the Strtotime () function to get the timestamp, and PHP converts the readable characters into a Unix timestamp. PHP is quite flexible in converting characters to timestamps, so you can insert a variety of values to get the time stamp you want.
This is a simple example:
Copy Code code as follows:
<?php
$mytime =strtotime ("7:50pm June 26 2011");
echo "Today is", date (' y-m-d g:i:sa ', $mytime);
?>
Output:
Today is 2011-06-26 7:50:00pm
PHP is pretty neat in interpreting characters, but not perfect, so be sure to test the characters you enter before you insert them. It's a great way to use "english-like instructions" to convert to the time stamp you need. You can do this as follows:
Copy Code code as follows:
$nextfriday =strtotime ("Next Friday"); Next Friday
$nextmonth =strtotime ("+1 Month"); Starting today, one months from now.
$lastchristmas =strtotime ("1 year Dec 25"); Last Christmas
Get date Range
The values returned by Strtotime are converted to numbers, and we can use these numbers to do the basic arithmetic, and we could do a lot of interesting things with these numbers. For example, you need to teach a subject every Tuesday for 16 weeks, and you want to get your teaching time. You can do the following things.
Copy Code code as follows:
<?php
$startdate = Strtotime (' next Tuesday ');
$enddate = Strtotime (' +16 weeks ', $startdate);
$currentdate = $startdate;
Echo ' <ol> ';
while ($currentdate < $enddate):
echo "\t<li>", Date (' M d ', $currentdate);
$currentdate = Strtotime (' +1 week ', $currentdate);
Endwhile;
Echo ' </ol> ';
?>
You will get the following results:
Copy Code code as follows:
Aug 21
Aug 28
Sep 04
Sep 11
Sep 18
SEP 25
Oct 02
Oct 09
OCT 16
OCT 23
OCT 30
Nov 06
Nov 13
Nov 20
Nov 27
Dec 04
Note this line: $currentdate = Strtotime ("+1 Week", $currentdate). In this line, you will find that you need to specify a timestamp as the second parameter, Strtotime will use this parameter instead of the default timestamp (today) and perform the operation.
Number of days to a certain date
When using a calculator, we will try to calculate the number of days in a day. You can easily calculate the timestamp of the fourth Thursday of November.
Copy Code code as follows:
$someday = Strtotime ("3 weeks Thursday November 1");
$daysUtilDate = Ceil (($someday-time ())/60/60/24);
echo "There are", $daysUtilDate, "until Thanksgiving";
First, we begin to calculate the Thanksgiving holiday (the 3rd Thursday after the first Thursday after November 1), and then we calculate the number of days between Thanksgiving and the current time through simple arithmetic. When we do the comparison, we can use time (), because it returns the number of ERAs in the current time.
It's a love affair.
If you start learning PHP, the learning date is the best way to learn the language, because the date has a lot of interesting things to do with you. Discover date and time related functions in the manual, please share your harvest for everyone.