PHP Notes: Introduction to the use of date functions_PHP Tutorial

WBOY
Release: 2016-07-21 15:11:41
Original
772 people have browsed it

Introduction

PHP is a very amazing language. It is powerful enough (the core language of the largest blog (WordPress)), it is widespread enough (runs on Facebook, the largest social networking site), and it is simple enough (as the preferred introductory language for beginners). Works well on low-cost machines. Moreover, the PHP language has many very good server packages (such as WAMP and MAMP), which are very convenient to install on your machine. PHP has very rich library resources, making it easy for developers to handle some businesses. Since we have the most contact with dates in the project, we will start learning from the date function today.

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




Getting started with dates in php5


date_default_timezone_set('Asia/Shanghai');
echo "Today is ",date('l');
?>



You will see the following content 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 parameter (this parameter tells us how to format the current date).

Try different formats

If you look at the PHP date function in the php manual, you will find that there are many ways to format dates.



will get

Today is 2012-08-17

Some dates are commonly used, so PHP provides some constants for you to use. For example, you can use cookies to get the client date.

You will get the following content

Today is Friday, 17-Aug-12 11:34:38 ​​CST Be careful not to use quotation marks when using constants.


What time is it now?

If you want to output the current time, you can use date (different formatting character parameters).

You will get

The time is 11:39:59am

Localize your time zone

If you find that the above code does not give the correct time, it is most likely because your server is set to a different time zone than your local time zone. You need to specify the time zone on the server, then you use the following code:



This will set the Shanghai time zone in China. This is a function of php5 (note older versions of php), there are many for you to choose the time zone. If you want it to be permanent, you can modify your php.ini file.

Get other times

You often need other times, not the current time. When you create a time using the date() function, the system uses Unix system time. This time represents the number of seconds since January 1, 1970 00:00:00 GMT (Unix Epoch Time) until now.

To specify how to get the date at a specified time, you can provide the number of seconds as the second parameter of the date(0 function.

The result is:

Today is 2011-06-27

This may seem useless, but it means you can use the date() function to do calculations. Before doing this, you need to simply create a timestamp.

Creating timestamps

There are many ways to create timestamps. We can use the mktime() function to get the timestamp we need.

Copy code The code is as follows:

$mytime=mktime(9, 23, 33, 6, 26, 2011);
echo "Today is ",date('Y-m-d g:i:sa', $mytime);
?>

The result is :

Today is 2011-06-26 9:23:33am mktime()

The function requires you to pass hours, minutes, seconds, months, days, and years in sequence. This is a good way to get a timestamp, but there are cooler ways.

Get the timestamp through characters

You can use the strtotime() function to get the timestamp, and PHP converts readable characters into Unix timestamps. PHP is quite flexible in converting characters into timestamps, so you can plug in all kinds of values ​​to get the timestamp you want.

This is a simple example:

Copy the code The code is as follows:

$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 slick at interpreting characters, but it's not perfect, so be sure to test the characters you enter before you insert them. Use "english-like instructions" to convert the required timestamp, which is a very good way. You can do it like this:

Copy the code The code is as follows:

$nextfriday=strtotime("next Friday"); //Next Friday
$nextmonth=strtotime("+1 Month"); //Calculate the time one month from today
$lastchristmas=strtotime("-1 year dec 25"); // Last Christmas

Get the date range

The value returned by strtotime is converted to a number. We can do basic operations with these numbers. We can use these numbers. Doing a lot of very interesting things. 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 The code is as follows:

$startdate = strtotime('next Tuesday');
$enddate = strtotime('+16 weeks', $startdate);
$currentdate = $startdate;
echo '
    ';
    while($currentdate < $enddate) :
    echo "t
  1. ", date('M d', $currentdate);
    $currentdate = strtotime('+1 week', $currentdate);
    endwhile;
    echo '
';
?>

You will get the following results:
Copy code The code is 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 Look at 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 the calculator, we will try to calculate the number of days to a certain day. You can easily calculate the timestamp of the fourth Thursday in November.

Copy code The code is as follows:

$someday = strtotime("3 weeks thursday November 1");
$ daysUtilDate = ceil(($someday - time())/60/60/24);
echo "There are ", $daysUtilDate, " until Thanksgiving";

First, let’s start Calculate the date of Thanksgiving (the third Thursday after the first Thursday after November 1), and then we use simple arithmetic to calculate the number of days between Thanksgiving and the current time. When we perform comparison operations, we can use time() because it returns the number of seconds since the epoch of the current time.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326804.htmlTechArticleIntroduction PHP is a very amazing language. It is powerful enough (the core language of the largest blog (wordpress)), it is broad enough (running on the largest social networking site facebook), it is enough...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template