Date string to Timestamp

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn. We can use the php built-in function strtotime to format the date as the timestamp, however, because my input box does not use the date control, you need to enter the date by yourself. In order to be more friendly to user input, a very basic Time Stamp conversion method is provided, regardless of user input: 20131122085959, 08:59:59, 2013-11.22085959, and so on can be formatted normally.
/**
* Date string to Timestamp
* Author: HongPing [hongping626@qq.com] 2013.11.22
* @ Param string $ date time string
* @ Return unixtime
*/
Function toUnix ($ date ){
If (empty ($ date )){
Return '';
}
$ FArr = str_split ('20140901'); $ dArr = str_split ($ date );
$ Fmdate = '';
Foreach ($ dArr as $ v ){
If (in_array ($ v, $ fArr )){
$ Fmdate. = $ v;
}
}
$ Year = (int) substr ($ fmdate, 0, 4); // year
$ Month = (int) substr ($ fmdate, 4, 2); // month
$ Day = (int) substr ($ fmdate, 6, 2); // day
$ Hour = (int) substr ($ fmdate, 8, 2); // hour
$ Minu = (int) substr ($ fmdate, 10, 2); // minute
$ Send = (int) substr ($ fmdate, 12, 2); // second
$ Hour = $ hour? $ Hour: 0; $ minu = $ minu? $ Minu: 0; $ send = $ send? $ Send: 0; $ month = $ month? $ Month: 0; $ day = $ day? $ Day: 0; $ year = $ year? $ Year: 0;
Return mktime ($ hour, $ minu, $ send, $ month, $ day, $ year );
// Return strtotime ($ date );
}

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.