Home >Backend Development >PHP Tutorial >PHP gets a certain time timestamp and mktime() function
mktime() function returns the Unix timestamp of a date. A Unix timestamp contains the number of seconds between the Unix epoch (January 1, 1970 00:00:00 GMT) and the specified time.
Grammar:
mktime(hour,minute,second,month,day,year);
Example:
<code><span><span><?php</span><span>$d</span> = mktime(<span>9</span>, <span>12</span>, <span>31</span>, <span>6</span>, <span>10</span>, <span>2015</span>); <span>echo</span><span>"创建日期是 "</span> . date(<span>"Y-m-d H:i:s"</span>, <span>$d</span>); <span>?></span></span></code>
Common timestamp function
<code><span>/** * 得到开始和结束unix时间段 */</span> public function get_start_and_end_time(<span>$mark</span>) { <span>if</span>(<span>$mark</span> == <span>'yesterday'</span>) { <span>$start_time</span> = mktime(<span>0</span>,<span>0</span>,<span>0</span>,<span>date</span>(<span>'m'</span>),<span>date</span>(<span>'d'</span>) - <span>1</span>,<span>date</span>(<span>'Y'</span>)); <span>$end_time</span> = mktime(<span>0</span>,<span>0</span>,<span>0</span>,<span>date</span>(<span>'m'</span>),<span>date</span>(<span>'d'</span>),<span>date</span>(<span>'Y'</span>)) - <span>1</span>; }<span>else</span><span>if</span>(<span>$mark</span> == <span>'today'</span>) { <span>$start_time</span> = mktime(<span>0</span>,<span>0</span>,<span>0</span>,<span>date</span>(<span>'m'</span>),<span>date</span>(<span>'d'</span>),<span>date</span>(<span>'Y'</span>)); <span>$end_time</span> = mktime(<span>0</span>,<span>0</span>,<span>0</span>,<span>date</span>(<span>'m'</span>),<span>date</span>(<span>'d'</span>) + <span>1</span>,<span>date</span>(<span>'Y'</span>)) - <span>1</span>; }<span>else</span><span>if</span>(<span>$mark</span> == <span>'toweek'</span>) { <span>$start_time</span> = mktime(<span>0</span>,<span>0</span>,<span>0</span>,<span>date</span>(<span>'m'</span>),<span>date</span>(<span>'d'</span>) - <span>date</span>(<span>'w'</span>) + <span>1</span>,<span>date</span>(<span>'Y'</span>)); <span>$end_time</span> = mktime(<span>23</span>,<span>59</span>,<span>59</span>,<span>date</span>(<span>'m'</span>),<span>date</span>(<span>'d'</span>) - <span>date</span>(<span>'w'</span>) + <span>7</span>,<span>date</span>(<span>'Y'</span>)); }<span>else</span><span>if</span>(<span>$mark</span> == <span>'tomonth'</span>) { <span>$start_time</span> = mktime(<span>0</span>,<span>0</span>,<span>0</span>,<span>date</span>(<span>'m'</span>),<span>1</span>,<span>date</span>(<span>'Y'</span>)); <span>$end_time</span> = mktime(<span>23</span>,<span>59</span>,<span>59</span>,<span>date</span>(<span>'m'</span>),<span>date</span>(<span>'t'</span>),<span>date</span>(<span>'Y'</span>)); } <span>$arr</span>[<span>'start_time'</span>] = <span>$start_time</span>; <span>$arr</span>[<span>'end_time'</span>] = <span>$end_time</span>; <span>return</span><span>$arr</span>; }</code>
The above introduces PHP to get a timestamp of a certain time and the mktime() function, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.