PHP notes: introduction to the use of date functions

  • 2020-05-30 19:46:07
  • OfStack

introduce

PHP is a very amazing language. It's powerful enough (the core language of the biggest blog (wordpress)), it's broad enough (it runs on the biggest social network, facebook), and it's simple enough (it's the language of choice for beginners). It works well on low cost machines. Also, php has some very nice server suites (WAMP and MAMP) that are easy to install on your machine. PHP has a very rich library that makes it easy for developers to do business. Since we work with dates the most in our projects, we'll start with date functions today.

Take a simple example of date

I'll output the content to our client (browser) using the echo command. I'll use the following code as the base code.


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Getting started with dates in php5</title>
</head>
<body>
<?php
date_default_timezone_set('Asia/Shanghai');
echo "Today is ",date('l');
?>
</body>
</html>

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

Try different formats

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

< ?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 Cookie to get the client date.

< ?php echo "Today is ",date(DATE_COOKIE); ? > You will get the following

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.

< ?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 code above doesn't give you the correct time, it's probably because your server has a different time zone than your own. You need to specify a time zone on the server, so you use the following code:

< ?php date_default_timezone_set('Asia/Shanghai'); ? >

This will set the time zone of Shanghai, China. This is a function of php5 (note the older version of php), and there are plenty of time zones to choose from. If you want to make it permanent, you can modify your php.ini file.

Get other times

You often need other time than the present time. When you create time using the date() function, the system will use Unix system time. This time represents the number of seconds from 00:00:00 on January 1, 1970, GMT (Unix era time) to the present time.

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

< ?php echo "Today is ",date('Y-m-d', 1309133434); ? > The result is:

Today is 2011-06-27

This doesn't seem very useful, but it means that you can do the calculation using the date() function. To do this, you need to simply create a timestamp.

Create timestamp

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


<?php 
    $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 in hours, minutes, seconds, months, days, years. This is a good way to get a timestamp, but there are cooler ways.

Get the timestamp by the character

You can get the timestamp using the strtotime() function, and php converts the readable characters into Unix timestamps. PHP is quite flexible in converting characters into timestamps, so you can insert various values to get the timestamp you want.

Here's a simple example:


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


$nextfriday=strtotime("next Friday"); // Next week, 5
$nextmonth=strtotime("+1 Month"); // Count from today 1 Months from now 
$lastchristmas=strtotime("-1 year dec 25"); // Last Christmas 

Get date range

The values that strtotime returns are converted to Numbers, and we can do basic operations with those Numbers, and we can do a lot of really interesting things with those Numbers. For example, if you need to teach a subject twice a week for 16 weeks, you want to get the time you need to teach the subject. You can do the following.

<?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 1 next line: $currentdate = strtotime("+1 week", $currentdate). In this line, you will find that you need to specify 1 timestamp as the second parameter. strtotime will use this parameter instead of the default timestamp (today) and perform the operation.

The number of days to a certain date

When we use a calculator, we try to calculate the number of days to a given day. You can easily calculate the time stamp for the 4th week of November.


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

First, we start to calculate the date of Thanksgiving (week 4 after November 1), and then we calculate the number of days between Thanksgiving and the present time through simple arithmetic. When we compare, we can use time(), because it returns the number of epoch seconds to the current time.


Related articles: