Home > Article > Backend Development > Is there a way to manipulate time in php?
php has a way to manipulate time. PHP provides a wealth of date and time processing methods: 1. date(), formats the local date and time; 2. mktime(), returns the timestamp of the date; 3. idate(), formats the local time as an integer; 4. , strtotime(), convert time string to timestamp, etc.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php has a method of operating time.
The use and processing of date and time are essential in web development, such as the time of form submission, the time of user login, the time of updating and deleting data in the database, etc. If you want to record the moment when these operations are performed, you need to complete it through date and time.
PHP provides us with a wealth of date and time processing functions:
Function | Description |
---|---|
checkdate() | Verifies the Gregorian date. |
date_add() | Add day, month, year, hour, minute and second to a date. |
date_create_from_format() | Returns a new DateTime object formatted according to the specified format. |
date_create() | Returns a new DateTime object. |
date_date_set() | Set a new date. |
date_default_timezone_get() | Returns the default time zone, used by all Date/Time functions. |
date_default_timezone_set() | Set the default time zone, used by all Date/Time functions. |
date_diff() | Returns the difference between two dates. |
date_format() | Returns the date formatted according to the specified format. |
date_get_last_errors() | Returns warnings/errors in the date string. |
date_interval_create_from_date_string() | Creates a DateInterval from the relevant part of the string. |
date_interval_format() | Format time interval. |
date_isodate_set() | Set ISO date. |
date_modify() | Modify the timestamp. |
date_offset_get() | Returns the time zone offset. |
date_parse_from_format() | Returns an associative array with details of the specified date according to the specified format. |
date_parse() | Returns an associative array with details for the specified date. |
date_sub() | Subtract the day, month, year, hours, minutes, and seconds from the specified date. |
date_sun_info() | Returns an array containing information about sunrise/sunset and dusk start/dusk end for the specified date and location. |
date_sunrise() | Returns the sunrise time of the specified date and location. |
date_sunset() | Returns the sunset time of the specified date and location. |
date_time_set() | Set the time. |
date_timestamp_get() | Returns the Unix timestamp. |
date_timestamp_set() | Sets the date and time based on a Unix timestamp. |
date_timezone_get() | Returns the time zone of the given DateTime object. |
date_timezone_set() | Set the time zone of the DateTime object. |
date() | Format local date and time. |
getdate() | Returns the date/time information of a timestamp or the current local date/time. |
gettimeofday() | Returns the current time. |
gmdate() | Format GMT/UTC date and time. |
gmmktime() | Returns the UNIX timestamp of the GMT date. |
gmstrftime() | Format GMT/UTC date and time according to locale settings. |
idate() | Format local time/date as an integer. |
localtime() | Returns the local time. |
microtime() | Returns the number of microseconds in the current Unix timestamp. |
mktime() | Returns the Unix timestamp of a date. |
strftime() | Format local time/date according to locale settings. |
strptime() | Parse the time/date generated by strftime(). |
strtotime() | Parses any English text date or time description into a Unix timestamp. |
time() | Returns the Unix timestamp of the current time. |
timezone_abbreviations_list() | Returns an associative array containing daylight saving time, offset, and time zone name. |
timezone_identifiers_list() | Returns a numeric array with all time zone identifiers. |
timezone_location_get() | Returns the location information of the specified time zone. |
timezone_name_from_ abbr() | Returns the time zone name based on the time zone abbreviation. |
timezone_name_get() | Returns the name of the time zone. |
timezone_offset_get() | Returns the time zone offset relative to GMT. |
timezone_open() | Create a new DateTimeZone object. |
timezone_transitions_get() | Returns all transitions for time zones. |
timezone_version_get() | Returns the version of the time zone database. |
The following is a brief introduction to some time and date processing methods:
1. PHP gets the current time (5 ways)
In date and time functions , it is very important to obtain the UNIX timestamp. The timestamp is a character sequence, which refers to the time from 00:00:00 on January 1, 1970 GMT (08:00:00 on January 1, 1970, Beijing time) to The current total number of milliseconds. Here are several functions to get the current time.
gmmktime()
The gmmktime() function obtains the UNIX timestamp of a GMT date. The syntax is as follows:
int gmmktime ([ int $hour [, int $minute [, int $second [, int $month [, int $day [, int $year [, int $is_dst ]]]]]]] )
The parameters of this function can be left empty from right to left, and the empty parameters will be set to the corresponding current GMT value.
Usage examples are as follows:
<?php echo gmmktime(); //没有设置参数, 则默认取得当前GMT时间 echo gmmktime(0,45,3,7,7,2016); //设置参数表示GMT时间2016年7月7日0点45分3秒 ?>
The print result of executing the above program is:
1467909956 1467852303
mktime()
mktime() can also get the UNIX timestamp of a date. The syntax is as follows:
int mktime ([ int $hour = date("H") [, int $minute = date("i") [, int $second = date("s") [, int $month = date("n") [, int $day = date("j") [, int $year = date("Y") [, int $is_dst = -1 ]]]]]]] )
This function returns a UNIX timestamp based on the parameters given. A timestamp is a long integer containing the number of seconds from the UNIX epoch to a given time.
Like the gmmktime() function, the parameters of this function can also be omitted from right to left. Any omitted parameters will be set to the current value of the local date and time.
Usage examples are as follows:
<?php echo mktime(); // 没有设置参数则默认取得当前GMT时间 echo mktime(0,45,3,7,7,2016); //设置参数表示GMT时间2016年7月7日0点45分3秒 ?>
The printed result of executing the above program is:
1467910465 1467852303
microtime()
microtime() gets the current UNIX timestamp and microseconds. The syntax is as follows:
mixed microtime ([ bool $get_as_float ] )
If you set the get_as_float parameter value to true, microtime() will return a floating point number; if there is no parameter, it will return a string in the "msec sec" format, where sec is the UNIX epoch since The number of seconds since now, msec is the microsecond part. Both parts of the string are returned in seconds.
Usage examples are as follows:
<?php echo microtime(); //返回 msec sec 格式字符串表示时间 echo "<br/>"; echo microtime(true); //返回一个浮点型字符串表示时间 ?>
The printing result of executing the above program is as follows:
0.40474900 1467910862 1467910862.4048
time()
The time() function returns the current UNIX timestamp. The syntax is as follows:
int time ( void )
time() The syntax of the function is relatively simple. The usage example is as follows:
<?php echo time(); ?>
The printed result of executing the above program is:
1467911104
getdate()
getdate() can obtain date and time information. The syntax is as follows:
array getdate ([ int $timestamp = time() ] )
This function returns an associative array array containing date information based on timestamp. If no timestamp is given, it is assumed to be the current local time (this is the same value as the time() function).
The key name units in the returned associative array are as shown in the table.
Key name | Description | Return value example |
---|---|---|
seconds | The number of seconds represents | 0 to 59 |
minutes | minutes The digits represent | 0 to 59 |
hours | The digits of hours represent | 0 to 23 |
mday | The number of the day of the month represents the week | 1 to 31 |
wday | The number of the day in the middle represents | 0 (Sunday) to 6 (Saturday) |
mon | The number of the month represents | 1 to 12 |
year | The complete year represented by 4 digits | For example: 1999 or 2019 |
yday | The number representing the day of the year | 0 to 365 |
weekday | The complete text representation of the day | Monday to Sunday |
month | The complete text representation of the month, such as January or April | January to December |
0 | The number of seconds since the UNIX epoch, similar to the return value of time() and the value used for date() | System dependent, typical value is -2147483648 ~ 2147483647 |
getdate() 函数的使用示例如下:
<?php echo "<pre class="brush:php;toolbar:false">"; var_dump(getdate()); ?>
执行以上程序的打印结果如下:
array(11) { ["seconds"]=>int(57) ["minutes"]=>int(18) ["hours"]=>int(17) ["mday"]=>int(7) ["wday"]=>int(4) ["mon"]=>int(7) ["year"]=>int(2016) ["yday"]=>int(188) ["weekday"]=>string(8) "Thursday" ["month"]=>string(4) "July" [0]=>int(1467911937) }
2、strtotime()与mktime():日期转时间戳
strtotime() 函数
使用 strtotime() 函数可以将任何字符串类型的日期/时间转换为 UNIX 时间戳,其语法格式如下:
strtotime($time [,$now = time()])
其中 $time 为表示时间/日期的字符串,例如“2020-01-01”;$now 为可选参数,用来计算返回值的时间戳。函数执行成功会返回指定的字符串,执行失败返回 FALSE。
另外需要注意的是,如果使用两位数字表示年份的话,其中 0 ~ 69 表示 2000 ~ 2069,70 ~ 100 则表示 1970 ~ 2000。
【示例】使用 strtotime() 函数获取指定时间的时间戳。
<?php echo '当前的时间戳是:'.time().'<br>'; echo '使用“now”获取当前的时间戳:'.strtotime('now').'<br>'; echo '2000-09-10 的时间戳是:'.strtotime("10 September 2000").'<br>'; echo '在当前的时间戳上加一天:'.strtotime("+1 day").'<br>'; echo '在当前的时间戳上加一周:'.strtotime("+1 week").'<br>'; echo '在当前的时间戳上加一周两天四小时两分钟:'.strtotime("+1 week 2 days 4 hours 2 seconds").'<br>'; echo '下一个周四的时间戳:'.strtotime("next Thursday").'<br>'; echo '上一个周一的时间戳:'.strtotime("last Monday").'<br>'; ?>
运行结果如下:
当前的时间戳是:1585273874 使用“now”获取当前的时间戳:1585273874 2000-09-10 的时间戳是:968515200 在当前的时间戳上加一天:1585360274 在当前的时间戳上加一周:1585878674 在当前的时间戳上加一周两天四小时两分钟:1586065876 下一个周四的时间戳:1585756800 上一个周一的时间戳:1584892800
注意:根据具体时间的变化,程序每次运行的结果略有不同。
mktime() 函数
除了 strtotime() 函数外,我们还可以使用 PHP 中的 mktime() 函数来获取指定日期的 UNIX 时间戳,该函数的语法格式如下:
mktime ([$hour = date("H") [, $minute = date("i") [, $second = date("s") [, $month = date("n") [, $day = date("j") [, $year = date("Y") [, $is_dst = -1 ]]]]]]])
参数说明如下:
$hour:表示一天中经过的小时数。如果 $hour 为负值,则表示前一天的适当时间;如果 $hour 大于 23,则表示第二天的适当时间。
$minute:表示一个小时内经过的分钟数。如果 $minute 为负值,则表示前一小时适当的分钟数;如果 $minute 大于 59,则表示下一小时中的适当分钟数。
$second:表示一分钟内经过的秒数。如果 $second 为负值,则表示前一分钟内的适当秒数;如果 $second 大于59,则表示下一分钟内的适当秒数。
$month:表示一年中经过的月份数,取值范围在 1 到 12 之间。如果 $month 小于 1(包括负值),则表示上一年的适当月份;如果 $month 大于 12,则表示下一年的适当月份。
$day:表示一个月中经过的天数,取值范围在 1 到 31 之间(取决于具体月份的天数)。如果 $day 小于 1(包括负值),则表示上个月的适当天数,比如 0 表示上个月的最后一天,-1 表示上个月的倒数第二天等等;如果 $day 大于本月的最大天数,则表示下个月的适当日期。
$year:表示具体的年份,可以是两位或四位数字,0 ~ 69 对应 2000 ~ 2069 年,70 ~ 100 对应 1970 ~ 2000年。在如今系统中普遍把 time_t 作为一个 32 位有符号整数的情况下,$year 的合法范围是 1901 到 2038 之间,不过此限制自 PHP5.1.0 起已被克服了。
$is_dst:本参数可以设为 1,表示正处于夏时制时间(DST),0 表示不是夏时制,或者 -1(默认值)表示不知道是否是夏时制。不过本参数在 PHP5.1.0 中已被废弃,并在 PHP7.0.0 中移除。
提示:mktime() 函数中的参数可以从右向左省略,任何省略的参数会被设置成本地日期和时间的当前值。如果省略函数的所有参数,那么 mktime() 函数会和 time() 函数一样,返回当前的时间戳。
【示例】使用 mktime() 函数获取指定时间的时间戳。
<?php $time1 = time(); echo '当前的时间戳是:'.$time1.'<br>'; $time2 = mktime(0, 0, 0, 3, 26, 20); echo '2020-03-26 00:00:00 的时间戳是:'.$time2.'<br>'; $time3 = mktime(); echo '省略函数的所有参数,获得的时间戳是:'.$time3.'<br>'; $time4 = mktime(-1, -20, 6, 3, 26, 2020); echo '2020-03-25 22:40:06 的时间戳是:'.$time4.'<br>'; ?>
运行结果如下:
当前的时间戳是:1585216256 2020-03-26 00:00:00 的时间戳是:1585152000 省略函数的所有参数,获得的时间戳是:1585216256 2020-03-25 22:40:06 的时间戳是:1585147206
推荐学习:《PHP视频教程》
The above is the detailed content of Is there a way to manipulate time in php?. For more information, please follow other related articles on the PHP Chinese website!