Date and time functions for PHP

Source: Internet
Author: User
Tags date current time getdate valid

1, Mktime () function:

The UNIX timestamp is returned based on the batch parameter, which needs to be formatted with the date () function to output the date and time.

Example: Use the Mktime () function to get the current time of the system

<?php
echo "Mktime function return value:". Mktime (). <br/> ";
echo "Current date time is:". Date ("Y-m-d h:i:s", Mktime ());
?>

2, Time () function: Returns the current UNIX timestamp

Cases:

<?php
echo "The return value of the time function:". Time (). <br/> ";
echo "Current time is:". Date ("Y-m-d h:i:s", Mktime ());
?>

3, Date () function: Gets the current date time

Date time can be exported in the format specified

Cases:

$time 1=date ("y-m-d h:i:s"); Get current time

4, GETDATE () function:

Gets information about the specified part of the date, and returns the date and time information in the array form.

Example: Show today is the first day of the year

<?php
$arr =getdate ();
echo $arr [' Year ']. -". $arr [' mon ']." -". $arr [' Mday ']." <br/> "; Output the current date
echo $arr [' hours ']. -". $arr [' minutes ']." -". $arr [' seconds ']." ". $arr [' Weekday ']; Output the current time information
echo "<br/>";
echo "Today is the first of the year". $arr [' yday ']. "Day"; Output today is the first day of the year
?>

5, Checkdate () function:

The Checkdate () function is a built-in date check function in PHP

The syntax is as follows:

BOOL Checkdate (int month,int day, int)

Where the valid value for month is the maximum number of days of the month for 1-12,day, and the valid value for year is 1-366

Cases:

<?php

$month = 2;
$day 1=29;
$day 2=30;
Var_dump (Checkdate ($month, $day 1, $year)); Returns True, February is only 29 days
echo "<br/>";
Var_dump (Checkdate ($month, $day 2, $year)); return False, February is only 29 days
?>

6, Strtotime () function:

function: Resolves a string of dates into a timestamp to compare the size of two times.

Time in PHP can not be directly compared, you need to first parse the time into a timestamp form, and then compare.

Cases:

<?php
$time 1=date ("y-m-d h:i:s"); Get current time
$time 2 = "2019-2-6 12:30:00";
if (Strtotime ($time 1)-strtotime ($time 2) >0) {//Compare two time
echo "\ $time 1 earlier than \ $time 2";
}else{
echo "\ $time 1 late than \ $time 2";
}
?>

Example: Using the Strtotime () function to develop a countdown program

<?php
$time 1=strtotime (Date ("y-m-d h:i:s")); Get current time
$time 2=strtotime ("2017-1-1"); New Year's Time
$sub =ceil (($time 2-$time 1)/86400); (60 seconds *60 * 24 hours)/day
echo "From New Year's Day and <font color= ' red ' > $sub </font> days! ";
?>
Related Article

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.