Date_default_timezone_set (' PRC '); Default time zone
- echo "Today:", Date ("Y-m-d", Time ()), "
";
- echo "Today:", Date ("Y-m-d", Strtotime ("June 2008")), "
";
- echo "Yesterday:", Date ("Y-m-d", Strtotime ("-1 Day")), "
";
- echo "Tomorrow:", Date ("Y-m-d", Strtotime ("+1 Day"), "
";
- echo "One week later:", Date ("Y-m-d", Strtotime ("+1 Week")), "
";
- echo "2 days a week four hours two seconds later:", Date ("Y-m-d g:h:s", Strtotime ("+1 Week 2 Day 4 hours 2 Seconds")), "
";
- echo "Next week Four:", Date ("Y-m-d", Strtotime ("next Thursday")), "
";
- echo "Previous Monday:". Date ("Y-m-d", Strtotime ("Last Monday")). "
";
- echo "One months ago:". Date ("Y-m-d", Strtotime ("Last month"). "
";
- echo "One months later:". Date ("Y-m-d", Strtotime ("+1 Month")). "
";
- echo "Ten years later:". Date ("Y-m-d", Strtotime ("+10 Year")). "
";
- $a = Date ("W", Time ()); Get today is what week of the week
- $startweekdate =date ("y-m-d h:i:s", Strtotime ($today)-($a-1) *24*3600); Get this week Monday
- $endweekdate =date ("y-m-d h:i:s", Strtotime ($today) + (7-$a + 1) *24*3600); Get the last day of the week and for the first day of next week
- $endnextweekdate =date ("y-m-d", Strtotime ($endweekdate) +7*24*3600);//The Last day of next week
- ?>
- $d = time (); Date to be processed
$w = Date ("W", $d); It's a day of the week.
- $d 0 = Date ("y-m-d", Strtotime ("-$w Day", $d)); Week start
- $d 6 = date ("y-m-d", Strtotime ((6-$w). "Day", $d)); Week End
- ?>
- $d = time (); Date to be processed
- $w = Date ("W", $d); It's a day of the week.
$d 0 = mktime (0,0,0,date ("M"), Date ("D")-$w, date ("Y"));//week start
- $d 0 = mktime (0,0,0,date ("M"), Date ("D")-$w + 6,date ("Y"));//End of week
- ?>
- $date = time ();
$w = Date ("W", $d);
- $d 0 = Date ("y-m-d", Strtotime ("-$w Day", $date)); Week start
- $d 6 = date ("y-m-d", Strtotime ((6-$w). "Day", $date)); Week End
- ?>
- $date 1 = date ("y-m-d", Strtotime ("Sunday")); Week start
- $date 2 = Date ("y-m-d", Strtotime ("Saturday")); Week End
- $date 1 = date ("y-m-d", Strtotime ("Last Sunday")); Started last week
- $date 2 = Date ("y-m-d", Strtotime ("Last Saturday")); Ended last week
- To get the system in the month
- $tmp _date=date ("Ym");
- Cut out year
- $tmp _year=substr ($tmp _date,0,4);
- Cut out Month
- $tmp _mon =substr ($tmp _date,4,2);
- $tmp _nextmnoth=mktime (0,0,0, $tmp _mon+1,1, $tmp _year);
- $tmp _forwardmnoth=mktime (0,0,0, $tmp _mon-1,1, $tmp _year);
- Get the next month of the current month
- $FM _next_mnoth=date ("Ym", $tmp _nextmonth);
- Get the last one months of the current month
- $FM _forward_mnoth=date ("Ym", $tmp _forwardmonth);
- ?>
- $d =array ("Day", "one", "two", "three", "four", "five", "six");
- $whatday = "Week". $d [Date ("W", Strtotime ($today))]; Get today's Day of the week
- $time = ABS ((strtotime ("2008-12-25")-Strtotime (Date ("y-m-d"))/86400);/get the number of days difference between two dates
- ?>
Copy CodeSee 7.3.6 date and time types for a description of the range of values that each type has and the valid format for the specified date and time value. Here is an example of using a date function. Select All records whose Date_col value is within the last 30 days:
- Mysql> SELECT something from table
- WHERE To_days (now ())-To_days (Date_col) <= 30;
Copy CodeDAYOFWEEK (date) Returns the day of the Week index (1= Sunday, 2 = Monday, ... 7= Saturday). These index values correspond to the ODBC standard.
- Mysql> Select DAYOFWEEK (' 1998-02-03 ');
- 3
Copy CodeWEEKDAY (date) Returns the day of the Week index (0= Monday, 1 = Tuesday, ...). 6= Sunday).
- Mysql> Select WEEKDAY (' 1997-10-04 22:23:00 ');
- 5
- Mysql> Select WEEKDAY (' 1997-11-05 ');
- 2
Copy CodeDayOfMonth (date) Returns the date of the month in the range of 1 to 31.
- Mysql> Select DayOfMonth (' 1998-02-03 ');
- 3
Copy CodeDayOfYear (date) returns the number of days in a year, in the range of 1 to 366.
- Mysql> Select DayOfYear (' 1998-02-03 ');
- 34
Copy CodeMonth (date) returns the months of date, ranging from 1 to 12.
- Mysql> Select MONTH (' 1998-02-03 ');
- 2
Copy CodeDayname (date) returns the week name of date.
- Mysql> Select Dayname ("1998-02-05");
- ' Thursday '
Copy CodeMONTHNAME (date) returns the month name of date.
- Mysql> Select MONTHNAME ("1998-02-05");
- ' February '
Copy CodeQUARTER (date) returns the quarter of a date year, ranging from 1 to 4.
- Mysql> Select QUARTER (' 98-04-01 ');
- 2
Copy CodeWEEK (date) WEEK (Date,first), where Sunday is the first day of the week, has a single parameter that returns the number of weeks of the date, ranging from 0 to 52. 2 Parameter Form week () allows you to specify whether the week starts in Sunday or Monday. If the second parameter is 0, the week starts from Sunday, if the second parameter is 1, starting from Monday.
- Mysql> Select WEEK (' 1998-02-20 ');
- 7
- Mysql> Select WEEK (' 1998-02-20 ', 0);
- 7
- Mysql> Select WEEK (' 1998-02-20 ', 1);
- 8
Copy CodeYear (date) returns the years of date, ranging from 1000 to 9999.
- Mysql> Select year (' 98-02-03 ');
- 1998
Copy CodeHOUR (time) returns the hour of time, ranging from 0 to 23.
- Mysql> Select HOUR (' 10:05:03 ');
- 10
Copy CodeMINUTE (time) returns the minute of time, ranging from 0 to 59.
- Mysql> Select MINUTE (' 98-02-03 10:05:03 ');
- 5
Copy CodeSECOND (time) returns the number of seconds in time, ranging from 0 to 59.
- Mysql> Select SECOND (' 10:05:03 ');
- 3
Copy CodePeriod_add (p,n) adds N months to the stage P (in format yymm or yyyymm). Returns a value in the format yyyymm. Note that the stage parameter p is not a date value.
- Mysql> Select Period_add (9801,2);
- 199803
Copy CodePeriod_diff (P1,P2) returns the number of months between the period P1 and P2, P1 and P2 should be in the format yymm or yyyymm. Note that the time parameters P1 and P2 are not date values.
- Mysql> Select Period_diff (9802,199703);
- 11
Copy CodeDate_add (Date,interval expr type) date_sub (date,interval expr type) adddate (date,interval expr type) subdate (date,in Terval expr type) These functions perform a date operation. For MySQL 3.22, they are new. Adddate () and subdate () are synonyms of date_add () and Date_sub (). In MySQL 3.23, you can use + and-instead of Date_add () and Date_sub (). (see example) date is a datetime or date value that specifies the start date, and expr is an expression that specifies the interval value to be subtracted from the start date or from the start date, and expr is a string; it can start with a "-" to indicate a negative interval. Type is a keyword that indicates how an expression should be interpreted. The EXTRACT (type from date) function returns the "type" interval from the date. The following table shows how the type and expr parameters are associated: type value meaning expected expr format SECOND sec SECONDS MINUTE min MINUTES HOUR time HOURS day days months month MONTHS year Year years Minute_second minutes and seconds "minutes:seconds" hour_minute hours and minutes "hours:minutes" Day_hour Day and Hour "Days HOURS" Year_month year and Month " Years-months "hour_second hours, minutes," HOURS:MINUTES:SECONDS "Day_minute days, hours, minutes" days hours:minutes "Day_second day, hour, minute, Seconds "Days HOURS:MINUTES:SECONDS" MySQL allows any punctuation delimiter in the expr format. Indicates that the suggested delimiter is displayed. If the date parameter is a date value and your calculation contains only the year, month, and day portions (that is, there is no time part), the result is a date value. Otherwise, the result is a datetime value.
- mysql> Select "1997-12-31 23:59:59" + INTERVAL 1 SECOND;
- -1998-01-01 00:00:00
- Mysql> Select INTERVAL 1 day + "1997-12-31";
- 1998-01-01
- mysql> Select "1998-01-01"-INTERVAL 1 SECOND;
- -1997-12-31 23:59:59
- Mysql> Select Date_add ("1997-12-31 23:59:59",
- INTERVAL 1 SECOND);
- -1998-01-01 00:00:00
- Mysql> Select Date_add ("1997-12-31 23:59:59",
- INTERVAL 1 day);
- -1998-01-01 23:59:59
- Mysql> Select Date_add ("1997-12-31 23:59:59",
- INTERVAL "1:1" minute_second);
- -1998-01-01 00:01:00
- Mysql> Select Date_sub ("1998-01-01 00:00:00",
- INTERVAL "1 1:1:1" day_second);
- -1997-12-30 22:58:59
- Mysql> Select Date_add ("1998-01-01 00:00:00",
- INTERVAL "-1" day_hour);
- -1997-12-30 14:00:00
- Mysql> Select Date_sub ("1998-01-02", INTERVAL);
- 1997-12-02
- Mysql> Select EXTRACT (Year from "1999-07-02");
- 1999
- Mysql> Select EXTRACT (year_month from "1999-07-02 01:02:03");
- 199907
- Mysql> Select EXTRACT (day_minute from "1999-07-02 01:02:03");
- 20102
Copy CodeIf you specify an interval value that is too short (excluding the desired interval portion of the type keyword), MySQL assumes that you omitted the leftmost part of the interval value. For example, if you specify a type of Day_second, the value expr is expected to have the day, hour, minute, and second parts. If you specify a value like "1:10", MySQL assumes that the day and hour portions are missing and the values represent minutes and seconds. In other words, the "1:10" Day_second is interpreted in the same way that it is equivalent to "1:10" minute_second, which means that MySQL interprets the time value as being two semantic in the way it has elapsed rather than as a day. If you use a date that is actually incorrect, the result is null. If you increase month, year_month, or year and the result date is greater than the maximum number of days for the new month, the days are adjusted with the largest day in the crescent.
- Mysql> Select Date_add (' 1998-01-30 ', Interval 1 month);
- 1998-02-28
Copy CodeNote that from the previous example morphemes interval and type keywords are not case-sensitive. To_days (date) gives a date, which returns a number of days (from 0 years).
- Mysql> Select To_days (950501);
- 728779
- Mysql> Select To_days (' 1997-10-07 ');
- 729669
Copy CodeTo_days () does not intend to use the value before the Gregory (1582) appears. From_days (n) gives a number of days N, which returns a date value.
- Mysql> Select From_days (729669);
- ' 1997-10-07 '
Copy CodeTo_days () does not intend to use the value before the Gregory (1582) appears. Date_format (Date,format) formats the date value according to the format string. The following modifiers can be used in the format string:%M month name (January ... December)%W week name (Sunday ... Saturday)%d The date of the month with English prefixes (1st, 2nd, 3rd, etc.). )%Y year, number, 4-digit%Y year, number, 2-bit%a abbreviated weekday name (Sun ... Sat)%d days in the month, number (00 ... %e number of days in the month, number (0 ... %m months, numbers (01 ... %c month, number (1 ... %b Abbreviated month name (Jan ... DEC)%j Days of the year (001 ... 366)%H hours (00 ... %k hours (0 ...) %h hours (01 ...) %I) Hours (01 ... %l hours (1 ...) %i minutes, Numbers (00 ...) %r time, 12 hours (Hh:mm:ss [ap]m)%T time, 24 hours (hh:mm:ss)%s seconds (00 ... ()%s seconds (00 ... %p am or PM%w one days of the week (0=sunday ... 6=saturday)%u week (0 ... 52), here Sunday is the first day of the week%u week (0 ... 52), here Monday is the first day of the week with a text "%". All other characters are not interpreted as being copied into the results.
- Mysql> Select Date_format (' 1997-10-04 22:23:00 ', '%W%M%Y ');
- ' Saturday October 1997 '
- Mysql> Select Date_format (' 1997-10-04 22:23:00 ', '%h:%i:%s ');
- ' 22:23:00 '
- Mysql> Select Date_format (' 1997-10-04-22:23:00 ',
- '%d%y%a%d%m%b%j ');
- 4th Sat Oct 277 '
- Mysql> Select Date_format (' 1997-10-04-22:23:00 ',
- '%H%k%I%r%T%s%w ');
- -10:23:00 PM 22:23:00 00 6 '
Copy CodeIn MySQL3.23,% is required before the format modifier character. In the earlier version of MySQL,% is optional. Time_format (Time,format) is used like the Date_format () function above, but the format string can contain only those formatting modifiers that handle hours, minutes, and seconds. Other modifiers produce a null value or 0. Curdate () current_date returns today's date value in ' Yyyy-mm-dd ' or YYYYMMDD format, depending on whether the function is used in a string or a numeric context.
- Mysql> select Curdate ();
- ' 1997-12-15 '
- Mysql> Select curdate () + 0;
- 19971215
Copy CodeCurtime () Current_time Returns the current time value in the ' HH:MM:SS ' or HHMMSS format, depending on whether the function is used in a string or in the context of a number.
- Mysql> select Curtime ();
- ' 23:50:26 '
- Mysql> Select Curtime () + 0;
- 235026
Copy CodeNow () sysdate () Current_timestamp returns the current date and time in ' Yyyy-mm-dd HH:MM:SS ' or YYYYMMDDHHMMSS format, depending on whether the function is used in a string or in the context of a number.
- Mysql> Select Now ();
- ' 1997-12-15 23:50:26 '
- Mysql> Select now () + 0;
- 19971215235026
Copy CodeUnix_timestamp () unix_timestamp (date) returns a UNIX timestamp (the number of seconds from ' 1970-01-01 00:00:00 ' GMT) if no argument is called. If Unix_timestamp () is called with a date parameter, it returns the value of the second starting at ' 1970-01-01 00:00:00 ' GMT. Date can be a date string, a DateTime string, a timestamp, or a number in the local time in YYMMDD or YYYYMMDD format. Mysql> select Unix_timestamp ();
- 882226357
- Mysql> Select Unix_timestamp (' 1997-10-04 22:23:00 ');
- 875996580
Copy CodeWhen Unix_timestamp is used in a timestamp column, the function will accept the value directly, without the implied "String-to-unix-timestamp" transform. From_unixtime (Unix_timestamp) returns the value represented by the YYYYMMDDHHMMSS parameter in the ' Yyyy-mm-dd HH:MM:SS ' or unix_timestamp format, depending on whether the function is in a string or used in a numeric context.
- Mysql> Select From_unixtime (875996580);
- ' 1997-10-04 22:23:00 '
- Mysql> Select From_unixtime (875996580) + 0;
- 19971004222300
Copy CodeFrom_unixtime (Unix_timestamp,format) returns a String representing the Unix time tag, formatted according to the format string. FORMAT can contain the same modifiers as the entries listed in the Date_format () function.
- Mysql> Select From_unixtime (Unix_timestamp (),
- '%Y%d%M%h:%i:%s%x ');
- ' 1997 23rd December 03:43:30 X '
Copy CodeSec_to_time (seconds) returns the seconds parameter, changing to hours, minutes, and seconds, formatted with a value of ' HH:MM:SS ' or HHMMSS, depending on whether the function is used in a string or in a numeric context.
- Mysql> Select Sec_to_time (2378);
- ' 00:39:38 '
- Mysql> Select Sec_to_time (2378) + 0;
- 3938
Copy CodeTime_to_sec (time) returns the time parameter, converted to seconds.
- Mysql> Select Time_to_sec (' 22:23:00 ');
- 80580
- Mysql> Select Time_to_sec (' 00:39:38 ');
- 2378
Copy Code |