We’ve already covered some of PHP’s date-manipulation objects, but today we’ll look at the rest of the process-oriented uses. Of course, we won’t cover methods that are similar to those in the DateTime class. In addition, Date() and time(), two very common functions, will not be covered because they are already overused, so we won’t waste valuable learning resources.

Check the date

First, let’s learn a function to see if the date is correct.

var_dump(checkdate(2.29.2020)); // bool(true)
var_dump(checkdate(2.29.2021)); // bool(false)
Copy the code

The year 2020 is a leap year, so there is February 29. 2021 has no February 29. The checkDate () function checks whether the given date is correct. The order of arguments is month, day, and year, all of which are required.

Gets and sets the time zone

In the procedural approach, we can also use two simple functions to get and set the time zone information of the current operating environment.

var_dump(date_default_timezone_get()); // string(13) "Asia/Shanghai"

var_dump(date("Y-m-d H:i:s")); // string(19) "2020-10-09 08:41:11"

date_default_timezone_set("Asia/Tokyo");

var_dump(date("Y-m-d H:i:s")); // string(19) "2020-10-09 09:41:11"
Copy the code

Use date_default_timezone_get() to obtain the timezone information in the current system environment, which is prioritized, with the timezone set using date_default_timezone_set() being the first. The time zone Settings in the php.ini file are followed by the time zone Settings in the php.ini file.

Format conversion date

In this case, converting dates means printing the date content as a detailed array.

print_r(date_parse("The 2020-12-12 10:00:00. 5"));
// Array
/ / (
// [year] => 2020
// [month] => 12
// [day] => 12
// [hour] => 10
// [minute] => 0
// [second] => 0
/ / [fraction] = > 0.5
// [warning_count] => 0
// [warnings] => Array
/ / (
/ /)

// [error_count] => 0
// [errors] => Array
/ / (
/ /)

// [is_localtime] =>
// )

$date = "6.1.2020 13:00 + 01:00";
print_r(date_parse_from_format("j.n.Y H:iP".$date));
// Array
/ / (
// [year] => 2020
// [month] => 1
// [day] => 6
// [hour] => 13
// [minute] => 0
// [second] => 0
// [fraction] => 0
// [warning_count] => 0
// [warnings] => Array
/ / (
/ /)

// [error_count] => 0
// [errors] => Array
/ / (
/ /)

// [is_localtime] => 1
// [zone_type] => 1
// [zone] => 3600
// [is_dst] =>
// )
Copy the code

Date_parse () and date_parse_from_format() are both functions that convert the date content into a detailed array. The difference is that the date_parse_from_format() function can specify a date and format. The date content passed in can then be of various format types. They generate arrays with very clear content field names, including year, month, hour, minute, error message, and so on.

Time the sun to rise and set

This function is more interesting. It returns the time when the sun rises and sets based on a date we specify.

$sun_info = date_sun_info(strtotime("2020-12-12"), 113.037211.28.203167);
foreach ($sun_info as $key= >$val) {
    echo "$key:" . date("H:i:s".$val)."\n";
}

// sunrise: 08:03:54
// sunset: 05:58:14
// transit: 19:01:04
// civil_twilight_begin: 09:58:56
// civil_twilight_end: 04:03:11
// nautical_twilight_begin: 11:20:07
// nautical_twilight_end: 02:42:01
// astronomical_twilight_begin: 12:27:37
// astronomical_twilight_end: 01:34:31
Copy the code

Sunrise is the sunrise, sunset sunset, is another twilight twilight and evening time, regardless of its accuracy, the function associated with sun and these functions are more interesting.

Get date and time details

As mentioned above, the date_parse() function converts the standard date format into a detailed array of date information, but there are other functions that do the same, and are much richer.

var_dump(getdate());
// array(11) {
// ["seconds"]=>
// int(15)
// ["minutes"]=>
// int(52)
// ["hours"]=>
// int(9)
// ["mday"]=>
// int(9)
// ["wday"]=>
// int(5)
// ["mon"]=>
// int(10)
// ["year"]=>
// int(2020)
// ["yday"]=>
// int(282)
// ["weekday"]=>
// string(6) "Friday"
// ["month"]=>
// string(7) "October"
/ / [0] = >
// int(1602204735)
/ /}

var_dump(gettimeofday());
// array(4) {
// ["sec"]=>
// int(1602205147)
// ["usec"]=>
// int(625261)
// ["minuteswest"]=>
// int(-540)
// ["dsttime"]=>
// int(0)
/ /}

var_dump(gettimeofday(true)); / / float (1602205147.6253)
Copy the code

The getDate () function returns only the date and time details, with no error messages. It not only contains the year, month, hour, and grade information, but also contains the timestamp information of the current time. It can be said that the true complete date details function. It can have an argument that returns the content of the specified date, or if it is not given, the current date and time is returned. Gettimeofday () returns the timestamp of the current date, as the name indicates. If set to true, it returns much the same value as time(), except that it returns microseconds. Microtime (), which we’ll see later, is a function that returns microsecond timestamps. It can also be set to true to return timestamps in this numeric format, though most people probably don’t know that.

Get local time details

$localtime = localtime();
$localtime_assoc = localtime(time(), true);
print_r($localtime);
// Array
/ / (
/ / [0] = > 14
/ / [1] = > 3
/ / [2] = > 10
/ / [3] = > 9
/ / [4] = > 9
/ / [5] = > 120
/ / [6] = > 5
/ / [7] = > 282
/ / [8] = > 0
// )
print_r($localtime_assoc);
// Array
/ / (
// [tm_sec] => 14
// [tm_min] => 3
// [tm_hour] => 10
// [tm_mday] => 9
// [tm_mon] => 9
// [tm_year] => 120
// [tm_wday] => 5
// [tm_yday] => 282
// [tm_isdst] => 0
// )
Copy the code

The localtime() function returns an array of information about the localtime, exactly as the C call returns, with the array keys in English if the second argument is set to true. As you can see, it is also the time information returned, including year, month, day, hour, minute, and second. It also includes the day of the week for Wday and the day of the year for yday.

Other time function

var_dump(microtime()); / / string (21) 0.38488800 1602205473 ""

var_dump(microtime(true)); / / float (1602205473.3849)
Copy the code

This is the microtime() function, which returns a timestamp in the form of microseconds, space seconds, with no arguments. Microseconds. Gettimeofday () is a more convenient way to concatenate your own timestamp format. After all, the array it returns already has the secs and usec fields ready for us. This is definitely the biggest surprise of today’s post, and can be applied to real business scenarios where we need microseconds.


var_dump(gmdate("Y-m-d H:i:s")); // string(19) "2020-10-09 01:00:20"

var_dump(idate('Y')); // int(2020)

var_dump(mktime(14.22.22.10.22.2020)); // int(1603344142)
var_dump(gmmktime(14.22.22.10.22.2020)); // int(1603376542)

var_dump(strftime("%C %Y %m %d %R %U")); // string(22) "20 2020 10 09 10:12 40"
var_dump(gmstrftime("%C %Y %m %d %R %U")); // string(22) "20 2020 10 09 01:13 40"

var_dump(strptime("The 2020-10-09 12:12:12".'%Y-%m-%d %H:%M:%S'));
// array(9) {
// ["tm_sec"]=>
// int(12)
// ["tm_min"]=>
// int(12)
// ["tm_hour"]=>
// int(12)
// ["tm_mday"]=>
// int(9)
// ["tm_mon"]=>
// int(9)
// ["tm_year"]=>
// int(120)
// ["tm_wday"]=>
// int(5)
// ["tm_yday"]=>
// int(282)
// ["unparsed"]=>
// string(0) ""
/ /}
Copy the code

Gmdate () takes the grytime, which is 8 hours less of our current time zone. The idate() function is used to get the specified time, such that we only get the current year in our test code.

Mktime () retrieves the timestamp of the specified time, and gmmktime() retrieves the timestamp of the specified time.

Strftime () and gmstrftime() fetch the formatted local time and date. The format that begins with gm is also the gray time, followed by the required format. This is similar to the format() method of DateTime, and the arguments are similar. %C is the century, and we need to add 1 to our actual century, like the code returns 20, and we’re actually in the 21st century. %R returns the H: I format, %U returns the week.

Strptime () returns an array of date details based on the date in the specified format, similar to localtime().

conclusion

The getTimeofday () function returns microseconds and is array formatted, as well as a function that can calculate the sunrise and sunset times of a specified date. Of course, learning these functions is just one thing. You need to remember that these functions are already available in PHP and then apply them in real business scenarios so that you can truly master them.

Test code:

Github.com/zhangyue050…

Reference Documents:

www.php.net/manual/zh/r…

Follow the public account: [Hardcore project manager] to get the latest articles

Add WeChat/QQ friends: free xiaoyuezigonggong / 149844827 】 【 PHP, project management, learning materials

Zhihu, Public Account, Douyin, Toutiao Search [Hardcore Project Manager]

ID of station B: 482780532