PHP Date Time processing function Summary _php tips

Source: Internet
Author: User
Tags explode
PHP calculates the number of hours and breaks it down
Copy Code code as follows:

/* Author: Yangyu yangyu@sina.cn * *
Parameter $hours_min array with a format of 1:10, returned to 1 hours
/*
For example:
$hours _min[0] = ' 1:10 ';
$hours _min[1] = ' 2:30 ';
Echo hours_sum ($hours _min);

The input is 4, which means total 4 hours.

*/
function Hours_sum ($hours _min) {

if (!is_array ($hours _min)) return false;

$tmp _arr = Array ();
foreach ($hours _min as $v) {
$tmp _arr = Explode (': ', $v);
$hour [] = $tmp _arr[0];
$min [] = $tmp _arr[1];
}

$hours = Array_sum ($hour);
$mins = Array_sum ($min);

$mins = $mins >= 10? Str_pad ($mins, 2, 0, str_pad_right): $mins;
$hours + + floor ($mins/60);
$hours + + $mins%60 >= 30? 1:0;
return $hours;
}

Convert a date to a week
Copy Code code as follows:

/* Author: Yangyu <yangyu@sina.cn> *
The input $data parameter is, YY/MM/DD or YY-MM-DD, returns the day of the week
function Getweekday ($date) {
$date = Str_replace ('/', '-', $date);
$DATEARR = Explode ("-", $date);
Return date ("N", Mktime (0,0,0, $DATEARR [1], $DATEARR [2], $DATEARR [0]));
}

PHP converts seconds to hours and minutes (format * * * hours * *)
Copy Code code as follows:

/* Author: Yangyu <yangyu@sina.cn>/*
//convert seconds (non time stamps) to * * * hours * * Nction Sec2time ($sec) {

$sec = round ($sec/60);
if ($sec >=) {
$hour = floor ($sec/60);
$min = $sec% 60;
$res = $hour. ' Hour ';
$min!= 0 && $res. = $min. ' Minute ';
}else{
$res = $sec. ' Minutes ';
}
Return $res;
}

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.