Introduction to PHP Date function learning notes

Source: Internet
Author: User
Tags arithmetic current time echo command time zones

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.

The code is as follows Copy Code

<! 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.

Today is Friday
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.

The code is as follows Copy Code
? <?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.

The code is as follows Copy Code
<?php echo "Today is", date (Date_cookie);?>

You will get 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).

The code is as follows Copy Code
<?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:
<?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.

The code is as follows Copy Code
<?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.

The code is as follows Copy Code
<?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:

The code is as follows Copy Code

<?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:

The code is as follows Copy Code
$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.

The code is as follows Copy Code

<?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:


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.

The code is as follows Copy Code
$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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.