Summary of php date and time processing functions

Source: Internet
Author: User
Php date and time processing function summary, hour, date, and so on. It is worth your reference. Merge php computing hours to zero

The code is as follows:


/* Author: Yang yuyangyu@sina.cn */
// The $ hours_min parameter is an array. the array format is and the return value is 1 hour.
/*
For example:
$ Hours_min [0] = '1: 10 ';
$ Hours_min [1] = '2: 30 ';
Echo hours_sum ($ hours_min );

Input 4, that is, 4 hours in total

*/
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 date to week

The code is as follows:


/* Author: Yang Yu */
// The input $ data parameter is yy/mm/dd or yy-mm-dd, and the returned day of the week is
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 (in the format of ** hours)

The code is as follows:


/* Author: Yang Yu */
// Converts a second (non-timestamp) to ** hour **.
Function sec2time ($ sec ){

$ Sec = round ($ sec/60 );
If ($ sec> = 60 ){
$ Hour = floor ($ sec/60 );
$ Min = $ sec % 60;
$ Res = $ hour. 'hour ';
$ Min! = 0 & $ res. = $ min. 'minutes ';
} Else {
$ Res = $ sec. 'mine ';
}
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.