PHP timestamp conversion

1, to obtain the current time method date ()
is very simple, this is the time of the acquisition method, format: date (format, format, timestamp ), format to format, timestamp is a timestamp - can fill parameters.

2, the method acquires a time stamp time (), strtotime ()
these two methods can be acquired in php unix timestamp, Time () to get direct access, strtotime (time, time, now ) is converted to the format Time stamp, $ time is required. Clearly this and want to learn more, please read on.

3, date ($ format) Usage
example:
echo DATE ( 'Ymd'), output: 2012-03-22
echo DATE ( 'Ymd H: I: S'), output: 2012-03-2223 : 00: 00
echo dATE ( 'Ym of-D', Time ()), output: 2012-03-22 23:00:00 (ibid result, only one more time stamp parameter) (converted to a date stamp format method)
... dATE echo ( 'the Y') 'on' .date ( 'm') 'month' .date ( 'd') 'day', output: March 22, 2012
for example on these a, but alternative format only, the following is the meaning of each letter format:
/ **** format may be used in the meaning of the letters **** /
a - "AM" or "PM"
a - "AM" or is "PM"
d - a few days, two figures, if less then two leading zeros; such as: "01" to "31"
d - day of the week, three letters; such as: "Fri"
F - month, English full name; described: "January"
H - 12 is hour clock; such as: "01" to "12 is"
H - 24 hour clock; such as: "00" to "23 is"
G - 12 is hour clock , zero padding is not less than two; such as: "1" to 12 is "
G - 24-hour clock, not less than two zero padding; as : "0" to "23 is"
I - min; such as: "00" to "59"
j - a few days, two figures, if less than two not zero padding; such as: "1" to "31"
L - day of the week, the English full name; such as: "catalog on Friday"
m - month, two figures, if less the two leading zeros; such as: "01" to "12 is"
n-- month, two digits, if the zero padding is not less than two; such as: "1" to "12 is"
M - month, three English letters; such as: "Jan"
S - seconds; such as: "00" to "59"
S - suffix plus the number of English sequence, two letters; such as: "TH", "Nd"
T - number of days specified month; as : "28" to "31"
U - total number of seconds
w - numeric day of the week, such as: "0" (Sunday) to "6" (Saturday)
the Y-- years, four digits; such as: "1999"
the y- - years, two figures; such as: "99"
z - the first day of the year; such as: "0" to "365"

4, strtotime ($ time) Usage
example:
echo strtotime ( '2012-03-22'), output: 1332427715 (where the result is just written only for instructions)
echo strtotime (DATE ( 'Yd of-m' )), the output :( combination date (), the result above) (date and time to a timestamp)
strtotime () there is a very strong usage, operating parameters can be added for numbers, date Zhouying Wen characters example as follows:
echo DATE ( 'Ymd H: I: S', strtotime ( 'Day +. 1')), output: 2012-03-23 23:30:33 (output time tomorrow will find time)
echo DATE ( 'Ymd H: i: s' , strtotime ( '- 1 day')), output: 2012-03-21 23:30:33 (last time in this case)
echo DATE ( 'Ymd H: I: S' , strtotime ( '+ 1 week' )), output: 2012-03-29 23:30:33 (At this time next week)
echo DATE ( 'Ymd H: I: S', strtotime ( 'Next catalog on Thursday ')), output: 2012-03-29 00:00:00 (At this time next Thursday)
echo DATE (' Ymd H: I: S ', strtotime (' Last catalog on Thursday ')), output: 2012-03-15 00:00:00 (last Thursday at this time of the time)
, and so I went to work Study it, strtotime () method can control the Unix timestamp English text display, the time required to obtain a date format.

. 5, the number of milliseconds php obtain the current time
php itself does not provide the function returns the number of milliseconds, but provides microtime, and () method, which returns a the Array, comprising two elements: a number of seconds, a number of milliseconds of decimal , we can obtain return the number of milliseconds through this method, as follows:
function getMillisecond () {
List (S1, S1, S2) = the explode ( '', microtime, and ());
. return (a float) sprintf ( '% 0F', (of floatval (S1) of floatval + (S1) of floatval + (S2)) * 1000);
}

6, get the current time difference of six hours workaround
some friends, get the time difference with the current system time of 6 hours, because the time zone setting issue, as long as it can be time to Shanghai. As follows:
1. Locate date.timezone in php.ini, its value is changed to Asia / Shanghai, i.e. Asia = date.timezone / of Shanghai
2. Add date_default_timezone_set ( 'Asia / Shanghai') at the beginning of the program to .

Get today, yesterday, last week, this month, this year's start timestamp and end timestamp

// php acquires a time stamp starting from today and end timestamp
$ = beginToday the mktime (0,0,0, DATE ( 'm'), DATE ( 'D'), DATE ( 'the Y'));
$ = endToday the mktime ( 0,0,0, date ( 'm') , date ( 'd') + 1, date ( 'Y')) - 1;


// php Get start timestamp and end timestamp yesterday
$ = beginYesterday the mktime (0,0,0, DATE ( 'm'), DATE ( 'D') -. 1, DATE ( 'the Y'));
$ endYesterday = mktime (0,0,0, date ( ' m'), date ( 'd'), date ( 'Y')) - 1;


// php Get last start timestamp and end timestamp
$ beginLastweek = mktime (0,0,0, date ( 'm'), date ( 'd') - date ( 'w') + 1-7, DATE ( 'the Y'));
$ = endLastweek the mktime (23,59,59, DATE ( 'm'), DATE ( 'D') - DATE ( 'W') + 7-7 of, DATE ( 'the Y') );


// php Get start timestamp and end timestamp Month
$ = beginThismonth the mktime (0,0,0, DATE ( 'm'),. 1, DATE ( 'the Y'));
$ = endThismonth the mktime (23,59 , 59, date ( 'm' ), date ( 't'), date ( 'Y'));

// Get the initial year and end timestamp
$ = beginThisyear the mktime (0,0,0,01,1, DATE ( 'the Y'));
$ = endThisyear the mktime (23,59,59,12, DATE ( 'T' ), date ( 'Y') );

PHP mktime () function returns a Unix timestamp date.

grammar

mktime(hour,minute,second,month,day,year,is_dst)

Parameter Description
hour optional. Provisions hour.
minute optional. Provisions minutes.
second optional. The provisions of sec.
month optional. It specifies the month with a digital representation.
day optional. The provisions of days.
year optional. Provisions of the. On some systems, the legal value between 1901-- 2038 between. However, this limit does not exist in PHP 5.
is_dst
optional. If the time period of daylight saving time (the DST), is set to 1, otherwise it is set to 0, if not known, is set to -1.

Since 5.1.0, is_dst parameters are discarded. Therefore, you should use the new time zone handling characteristics.

 

usage

Parameters always represent GMT date, therefore is_dst no effect on the results.

Parameters can be from right to left empty, empty parameters will be set to the current corresponding GMT value.

Note Before PHP 5.1, if the argument of the function is illegal, it will return false.

Also note that the function is very useful for verification and calculation date. It can automatically correct out of range input, such as:


1 echo (date ( "MdY" , mktime (0,0,0,12,36,2001)));
outputting the results:

Jan-05-2002

 

two,

 

// Get today 00:00
$ todaystart = strtotime (DATE ( 'Ym-d' '00: 00: 00', Time ()).);
// Get today 24:00
$ todayend = strtotime (DATE ( 'Ym '.' -d 00: 00: 00 ', Time () * + 24-3600));
// statistics today registered user
$ todayuser [' create_time '] = array (between, "$ todaystart, $ todayend");
$ todaysum = $ Users-> where ( $ todayuser) -> count ();


// get yesterday 00:00
$ TimeStart = strtotime (DATE ( 'Ym-d'. '00: 00: 00', Time () - * 24-3600));
// Get today 00:00
$ TimeEnd = strtotime ( date (. 'Ym-d' '00: 00: 00', time ()));

Guess you like

Origin www.cnblogs.com/camg/p/11980214.html