Code for comparing two dates in php

Source: Internet
Author: User
Tags comparison current time explode getdate local time readable string format valid
The code is as follows: Copy code

<? Php
/*
* Author: I don't know who the author is.
* Date: 2008-4-13
* Function: calculate the time difference between two dates.
* Note: the program code is basically not modified. Only the writing specification is defined, and comments are made. // mancharro
*/
  
// Define the two dates to be compared
$ Date_1 = "2008-7-15 ";
$ Date_2 = "2006-7-14 ";

// Convert the date into an array to create a timestamp
$ Date_List_1 = explode ("-", $ Date_1 );
$ Date_List_2 = explode ("-", $ Date_2 );

// Create a timestamp
$ D1 = mktime (0, 0, 0, $ Date_List_1 [1], $ Date_List_1 [2], $ Date_List_1 [0]);
$ D2 = mktime (0, 0, $ Date_List_2 [1], $ Date_List_2 [2], $ Date_List_2 [0]);

// Date comparison
$ Days = round ($ d1-$ d2)/3600/24 );

// Output result
Echo "$ Days days ";
?>

Method 2

The code is as follows: Copy code

<? Php
/*
* Author: mamchataro
* Date: 2008-4-13
* Function: calculate the time difference between two dates.
*/

// Use the strtotime function provided by php to format the date timestamp
$ Date0000ts = strtotime ");
$ Date2_ts = strtotime ("2006-7-14 ");

// Date comparison
$ Days = round ($ date0000ts-$ date2_ts)/3600/24 );

// Output result
Echo "$ days ";
?>

Function

Description

Instance

                 

Checkdate ($ month, $ date, $ year)

If the value of the application constitutes a valid date, the function returns true. For example, if the error date is January 1, February 31, 2005, this function returns false.

       

You can use this function to check the date and make it take effect before the date is used for calculation or storage in the database.

<? Php

       

// Returns false

       

Echo checkdate (2,30, 2005 )?

       

"Valid ":

       

"Invalid ";

       

 

       

// Returns true

       

Echo checkdate (2010 )? "Valid ":

       

"Invalid ";

       

?>

Getdate ($ ts)

If no independent variable exists, this function returns the current date and time in combination with an array. Each element in the array represents a specific component of the date/time value. You can submit optional time tag independent variables to the function to obtain the date/time value corresponding to the time tag.

       

Use this function to obtain a series of discrete and easily separated date/time values.

<? Php

       

// Returns timestamp

       

13:15:23

       

7-Jun-2006

       

Echo mktime );

       

?>

Mktime ($ hour,

       

$ Minute,

       

$ Second,

       

$ Month, $ day,

       

$ Year)

Function andGetdate ()Instead: it generates a UNIX time tag (the number of seconds since January 1, January 1, 1970 GMT) from a series of date and time values ). When no independent variable is needed, it generates the UNIX time tag for the current time.

       

Use this function to obtain the UNIX time tag of real-time. This time label is usually used in many databases and programming languages.

<? Php

       

// Returns timestamp

       

13:15:23

       

7-Jun-2006

       

Echo mktime );

       

?>

       

Date ($ format, $ ts)

This function format the UNIX time tag into a readable date string. It is the most powerful function in the PHP date/time API and can be used in a series of correction values to convert the integer time tag to the desired string format.

       

 

       

Apply this function to display the formatting time or date.

<? Php

       

// Format current date

       

// Returns "13-Sep-2005 PM"

       

Echo date ("d-M-Y h: I A", mktime

       

());

       

?>

       

Strtotime ($ str)

This function converts readable English date/time strings into UNIX time labels.

       

Use this function to convert non-standard date/time strings into standard and compatible UNIX time labels.

<? Php

       

// Returns 13-Sep-05

       

Echo date ("d-M-y ",

       

Strtotime ("today "));

       

 

       

// Returns 14-Sep-05

       

Echo date ("d-M-y ",

       

Strtotime ("tomorrow "));

       

 

       

// Returns 16-Sep-05

       

Echo date ("d-M-y", strtotime ("today + 3

       

Days "));

       

?>

                 

Strftime ($ format, $ ts)

As defined by the setlocale () function, this function formats the UNIX time tag to a date string applicable to the current environment.

       

Use this function to create a date string compatible with the current environment.

<? Php

       

// Set locale to France (on

       

Windows)

       

Setlocale (LC_TIME, "fra_fra ");

       

 

       

// Format month/day names

       

// As per locale setting

       

// Returns "septembre" and

       

"Mardi"

       

Echo strftime ("Month: % B ");

       

Echo strftime ("Day: % ");

       

?>

Microtime ()

This function returns the number of seconds and microseconds that have elapsed since January 1, January 1, 1970, GMT.

       

This function is applied when the execution time of a specific code block of the benchmark is accurately measured.

<? Php

       

// Get starting value

       

$ Start = microtime ();

       

 

       

// Run some code

       

For ($ x = 0; $ x <1000; $ x ++ ){

       

??? $ Null = $ x * $ x;

       

}

       

 

       

// Get ending value

       

$ End = microtime ();

       

 

       

// Calculate time taken

       

Code

       

Execution

       

Echo "Elapsed time:". ($ end-

       

 

       

$ Start). "sec ";

       

?>

Gmmktime

       

($ Hour,

       

$ Minute,

       

$ Second,

       

$ Month, $ day,

       

$ Year)

This function generates a UNIX time tag by a series of date and time values expressed in GMT. When no independent variable is needed, it generates a UNIX time tag of the current GMT real-time.

       

This function is used to obtain UNIX time tags of GMT real-time.

<? Php

       

// Returns timestamp

       

12:25:23

       

9-Jul-2006

       

Echo gmmktime

       

);

       

?>

                 

Gmdate ($ format, $ ts)

This function formats UNIX time tags into readable date strings. This date string is expressed in GMT (non-local time.

       

This function is applied when the time tag is expressed in GMT.

<? Php

       

// Format current date into GMT

       

// Returns "13-Sep-2005 08:32

       

AM"

       

Echo gmdate ("d-M-Y h: I ",

       

Mktime ());

       

?>

Date_default _

       

Timezone_set

       

($ Tz) and

       

Date_default _

       

Timezone_get ()

All date/time function calls after this function are set and the default time zone is restored.

       

Note: This function is only valid in PHP 5.1 +.

       

This function is a convenient shortcut for future time operations.

<? Php

       

// Set timezone to UTC

       

Date_default_timezone_set

       

('Utc ');

       

?>

 

 

 

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.