Collected several common PHP date operations

Source: Internet
Author: User
Tags explode time and seconds

Here the date action function has an increase in time, calculated the date given is the day of the week, check the date is valid date time comparison function, return two date difference a few seconds, a few minutes, a few hours or a few days

<?php
function DateAdd ($date, $int, $unit = "D") {

The increase in time (can also be improved as time and seconds can be increased, there is time to fill up)
$datearr = Explode ("-", $date);
$value [$unit] = $int;

//

Return date ("Y-m-d", Mktime (0,0,0, $datearr [1] + $value [' m '], $datearr [2] + $value [' d '], $datearr [0] + $value [' y ']));

Cloud Habitat Community Network

}

function Getweekday ($date) {//Calculate the date given is the day of the week
$datearr = Explode ("-", $date);
Return Date ("W", Mktime (0,0,0, $datearr [1], $datearr [2], $datearr [0]));

//

}
?>


?
function Check_date ($date) {//check date is legal date
$datearr = Explode ("-", $date);
if (is_numeric ($datearr [0]) && is_numeric ($datearr [1]) && is_numeric ($datearr [2])) {//cloud-Habitat Community Network
return Checkdate ($datearr [1], $datearr [2], $datearr [0]);
}
return false;
}
function Check_time ($time) {//Check time is legal time

Cloud Habitat Community Network

$timearr = Explode (":", $time);
if (is_numeric ($timearr [0]) && is_numeric ($timearr [1]) && is_numeric ($timearr [2])) {

//

if ($timearr [0] >= 0 && $timearr [0] <=) && ($timearr [1] >= 0 && $timearr [1] <= 59) && ($timearr [2] >= 0 && $timearr [2] <= 59))

Php

return true;
Else
return false;
}
return false;
}


function DateDiff ($date 1, $date 2, $unit = "") {

A time comparison function that returns two dates, a few seconds, minutes, hours, or days.
//
Switch ($unit) {
Case ' s ':
$dividend = 1;
Break
Case ' I ':
$dividend = 60;

            break;
        case ' h ':
$dividend = 3600;
            break;
        case ' d ':
$dividend = 86400;
            break;//
         default:
$dividend = 86400;
   }
$time 1 = strtotime ($date 1);
$time 2 = strtotime ($date 2);
    if ($time 1 && $time 2)//
        return (float) ( $time 1-$time 2)/$dividend;
    return false;
}

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.