This morning, I asked a question about the mysql database storage time. I suddenly thought about the date and time in php and mysql. In a project, we often use multiple expressions for the same time. For example, I used int (11) to store unix timestamps in mysql databases, use the date () function for format after the query.
This morning, I asked a question about the mysql database storage time. I suddenly thought about the date and time in php and mysql. In a project, we often use multiple expressions for the same time. For example, I used int (11) to store unix timestamps in mysql databases, use the date () function for format after the query.
This morning, I asked a question about the mysql database storage time. I suddenly thought about the date and time in php and mysql.
In a project, we often use multiple expressions for the same time. For example, I used int (11) to store unix timestamps in mysql databases, after the query is made, format it with the date () function, or use the 'tuesday 18th March 123456', '2014/1/123456' (US format), '2014/123456' (European format ), '123' and so on. How can we select so many time formats? How can we display the list of articles obtained from the database and sort them by date? What should I do if I want to present more complex content, such as online calendars?
However, before that, we need to explain that we have many time zones on the whole earth, not to mention where we use the server, which will display the time zone, if we do not set it, the default value is the time in the 0 time zone, which is 8 hours different for us. However, you can use date_default_timezone_get () to obtain the time zone of the server. You can use date_default_timezone_set (string$timezone_identifier
) Set the time zone, such as date_default_timezone_set ("Asia/Shanghai");, then the server's time zone is changed to UTC + 8.
UNIX timestamp:What is unix is a timestamp, and unix timestamp is the number of seconds that have elapsed from a standard time point (00:00:00) to a certain point, such as now (15:20:47) the timestamp is 1365664847. Php has many functions that operate on timestamps, such as time (), mktime (), date (), strtotime (), and microtime.
For time () Functions: Int time (void), returns the number of seconds (Unix timestamp) from the unix epochs (January 1, 1970 00:00:00 Greenwich Mean time) to the current time ). We can see that the time () function has no parameter and returns the unix timestamp of the current time.
Mktime (): Int mktime (int $ hour, int $ minute, int $ second, int $ month, int $ day, int $ year), returns the unix timestamp. The input parameters can be omitted from right to left. If omitted, a value of the current time is used by default. For example, if $ year is omitted, the default value is 2013.
Date (): String date (string format, int timestamp), returns the time format after the integer timestamp is formatted with format, timestamp can be omitted, if this parameter is omitted, the current timestamp (time () is used by default ()).
Strtotime (): This function is very powerful and can be understood through examples.
Microtime (): Returns the unix Timestamp and microsecond Of the current time.
Let's try the above functions to see what can be output.
1 //date_default_timezone_set("Asia/Shanghai"); 2 echo 'date_default_timezone:'.date_default_timezone_get().'
'; 3 4 5 echo 'time():'.time().'
'; 6 7 echo 'date("Y/m/d H:i:s"):'.date('Y/m/d H:i:s').'
'; 8 echo 'date("Y/m/d H:i:s", 1365665148)'.date("Y/m/d H:i:s", 1365665148).'
'; 9 10 echo 'microtime():'.microtime().'
';11 echo 'microtime(true):'.microtime(true).'
';12 13 echo 'mktime(14, 43, 0, 4, 11, 2013):'.mktime(14, 43, 0, 4, 11, 2013).'
';14 echo 'mktime(14, 43, 0, 4):'.mktime(14, 43, 0, 4).'
';15 16 echo 'strtotime("April 11th 2013 14:43:00"):'.strtotime('April 11th 2013 14:43:00').'
';17 echo 'strtotime("April"):'.strtotime('April').'
';18 echo 'strtotime("+1 day"):'.date("Y/m/d l H:i:s", strtotime('+1 day')).'
';19 echo 'strtotime("-1 month"):'.date("Y/m/d l H:i:s", strtotime('-1 month')).'
';20 echo 'strtotime("-2 year"):'.date("Y/m/d l H:i:s", strtotime('-2 year')).'
';21 echo 'strtotime("next friday"):'.date("Y/m/d l H:i:s", strtotime('next friday')).'
';22 echo 'strtotime("last saturday"):'.date("Y/m/d l H:i:s", strtotime('last saturday')).'
';
Is strtotime very powerful? It can convert some time strings that can be judged into timestamps.
Is there an impulse to make a simple calendar? Let's try it. We will do the following.
First, our preparations include:
(1) The timestamp of The First Day (1) of a month. The timestamp is used to obtain the time nodes of the previous month, next month, and previous year and next year of the month;
(2) to display the complete calendar of a month, we need to: 1 There are several blank days before Sunday (if Sunday is the first day of a month ), the total number of days of the month. If you want to get a link back to today, you need to get the date of today;
(3) The link jump can be set to pass parameters to the url, and then the page displays the corresponding calendar based on the obtained parameters;
(4) link creation for the previous year: year of the previous year and current month; Link creation for the next year: Year of the next year and current month;
(5) create a link for the previous month: the year of the previous month and the month of the previous month; create a link for the next month: the year of the next month and the month of the next month. Why can't we use the current year directly? You know.
(6) Fill in the table according to the format and then output it.
(7) completed.
Let's write it step by step and define an array $ calender_data to store all the data in the array, including an empty lattice.
Obtain the timestamp of the first day of the current month and generate the timestamp of the last month, the next month, the last year, and the next year:
1 $ g =$ _ REQUEST; 2 $ year = isset ($ g ['Year'])? $ G ['Year']: date ("Y"); // get $ year 3 $ month = isset ($ g ['month'])? $ G ['month']: date ("F"); // get $ month 4 $ start_day = strtotime ("{$ month} 1 {$ year }"); // $ year $ month the first day of the timestamp 5 $ day_num = date ('T', $ start_day); // the number of days of a month 6 $ date_range = range (1, $ day_num); // 7 $ pre_month = strtotime ("-1 month", $ start_day ); // The first day of last month's timestamp 8 $ next_month = strtotime ("+ 1 month", $ start_day ); // The first day of the next month's timestamp 9 $ pre_year = strtotime ("-1 year", $ start_day); // 10 $ next_year = strtotime ("+ 1 year ", $ start_day );
The range () function creates and returns an array containing elements of the specified range.
Generate links for the last month, next month, last year, and next year:
1 define(EMP, ' '); 2 $html = "$_SERVER['SCRIPT_NAME']}?year=%s&month=%s'>%s"; 3 $calender_data = array(); 4 $calender_data[] = sprintf($html, date('Y', $pre_month), $month, date('Y', $pre_year)); 5 $calender_data[] = EMP; 6 $calender_data[] = EMP; 7 $calender_data[] = $year; 8 $calender_data[] = EMP; 9 $calender_data[] = EMP;10 $calender_data[] = sprintf($html, date('Y', $next_year), $month, date('Y', $next_year));11 12 $calender_data[] = sprintf($html, date('Y', $pre_month), date('F', $pre_month), date('M', $pre_month));13 $calender_data[] = EMP;14 $calender_data[] = EMP;15 $calender_data[] = $month;16 $calender_data[] = EMP;17 $calender_data[] = EMP;18 $calender_data[] = sprintf($html, date('Y', $next_month), date('F', $next_month), date('M', $next_month));
Generate weekly Abbreviation:
1 $calender_data[] = 'Sun';2 $calender_data[] = 'Mon';3 $calender_data[] = 'Tue';4 $calender_data[] = 'Wed';5 $calender_data[] = 'Thu';6 $calender_data[] = 'Fri';7 $calender_data[] = 'Sat';
Append the space before a month and the last space to the array:
1 $start_blank = date('N', $start_day)%7; 2 for($i=0; $i<$start_blank; $i++){ 3 $calender_data[] = EMP; 4 } 5 foreach($date_range as $v){ 6 $calender_data[] = $v; 7 } 8 $end_blank = (7-($day_num+$start_blank)%7)%7; 9 for($i=0; $i<$end_blank; $i++){10 $calender_data[] = EMP;11 }
Fill the array in the table:
1 $h = '
';2 for($i=0, $t=count($calender_data); $i<$t; $i++){3 $h .= "
{$calender_data[$i]} | ";4 if(($i+1)%7==0){5 $h .= "
";6 }7 }8 $h .= '
';9 echo $h;
Okay. Now the entire calendar is complete.