PHP implements a method to compare two string date sizes _php
Source: Internet
Author: User
KeywordsPHP Comparison string date
The example in this article describes how PHP implements a comparison of two string date sizes. Share to everyone for your reference. Specific as follows:
<?phpfunction datebdate ($date 1, $date 2) {//date 1 is greater than date 2 $month 1 = date ("M", Strtotime ($date 1)), $month 2 = Date ("M", STRT Otime ($date 2)); $day 1 = date ("D", Strtotime ($date 1)); $day 2 = date ("D", Strtotime ($date 2)); $year 1 = date ("Y", Strtotime ($date 1)); $year 2 = Date ("Y", Strtotime ($date 2)); $from = mktime (0, 0, 0, $month 1, $day 1, $year 1); $to = mktime (0, 0, 0, $month 2, $day 2, $year 2); if ($from > $to) {return true;} else {return false;}} ?> $date 1 = "2009-10-13"; $date = mktime (0, 0, 0, date ("M", Strtotime ($date 1)), date ("D", Strtotime ($date 1)), date ("Y", s Trtotime ($date 1)));
Finally get a date for the Unix timestamp $date=1255392000.
Many times when searching, the search time can not be greater than the current date, the comparison function is roughly the same as the above one function, as follows:
function Datebcurrent ($date) {//date is greater than the current date $currentDate =date ("y-m-d");//Gets the current date $cYear =date ("Y", Strtotime ($ currentdate)); $cMonth =date ("M", Strtotime ($currentDate)); $cDay =date ("D", Strtotime ($currentDate)); $year =date ("Y", Strtotime ($date)); $month =date ("M", Strtotime ($date)); $day =date ("D", Strtotime ($date)); $currentUnix =mktime (0,0,0, $cMonth, $cDay, $cYear); Unix timestamp for the current date $dateUnix =mktime (0,0,0, $month, $day, $year); The Unix timestamp for the date to compare if ($dateUnix <= $currentUnix) {return true;} else{return false;}}
I hope this article is helpful to everyone's PHP programming.
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.