Example Introduction PHP Date function Conversion _php Tutorial

Source: Internet
Author: User
Tags echo date time zones iso 8601 iso 8601 format php example
Web site development We often need to deal with the date, time, in PHP provides a lot of date, time function to facilitate the PHP developers to calculate the date, time, format conversion. So it is necessary to master the PHP date function and to lay the groundwork for processing the format conversion between the PHP date function and the MySQL database date. Here is a php example of the PHP Foundation: Date function dates and the UNIX timestamp function and the date format of the conversion method.

PHP Date Time zone setting

Before you enable the PHP date function, you first need to make sure that the time zone you set is correct, otherwise the displayed date may differ. In the PHP environment, you only need to set the Date.timezone in the php.ini configuration file to the appropriate time zone. If you do not have the right to manipulate the php.ini profile, you can get the time zone of the current PHP runtime by using the PHP time zone function date_default_timezone_get (void), and then use the Date_default_timezone_set ( The string $timezone _identifier) function sets the appropriate time zone, and more time zones supported by PHP can be queried for http://www.php.net/manual/en/timezones.php.

PHP formatted date function dates

Prototype string Date (string $format [, int $timestamp])

$format the format code for date function-php
$timestamp-unix timestamp, default current timestamp (optional)

Format character ———-Description ——— return value description

Days (Day)

1
d--Day of the month, returns 2 digits--01 to 31
2
The day of the j--month, 1 to 9 without 0--1 to 31
3
s--The English suffix of the number of days per month-2 characters st,nd,rd or th. Can be used with J.
4
z--the day of the year--0 to 366


Week (Week)

1
l--weeks--sunday to Saturday
2
d--English Week, text representation--3 letters Mon to Sun
3
n--the day of the week (PHP 5.1.0 New)--1 (representing Monday) to 7 (for Sunday)
4
w--Day of the week--0 (indicated Sunday) to 6 (indicated Saturday)
5
Week of the w--iso-8601 format year, starting from Monday (PHP 4.1.0 New)--42 (42nd Week of the year)


Monthly (month)

1
f--month--january or March January to December
2
m--number represents the month--01 to 12
3
m--three-letter abbreviation for month--jan to Dec
4
n--number represents the month--1 to 12
5
t-– month days--28 to 31


Years (year)

1
l--leap Year, expressed as a Boolean value--if it is a leap year of 1, otherwise 0
2
o--iso-8601 format year number. Similar to Y, unless the day of the Week (W) of the ISO belongs to the previous or next year, then that year. (PHP 5.1.0 New)--1999 or 2003
3
y--Four-digit year full representation of--1999 or 2003
4
y--Two-digit year--99 or 03


Times (Time)

1
a--morning and afternoon--am or PM
2
a--morning and afternoon--am or PM
3
B--swatch Internet time--000 to 999
4
g--hours, 12-hour format--no leading 1 to 12
5
g--hours, 24-hour format--no leading 00 to 23
6
h--hours, 12-hour format--with leading 1 to 12
7
h--hours, 24-hour format--with leading 000 to 23
8
i--min--00 to 59
9
s--seconds--00 to 59


Time Zone (Timezone)

1
e--time zone ID (PHP 5.1.0 new Plus)--utc,gmt,atlantic/azores
2
i--is daylight saving time--daylight saving time is 1, otherwise 0
3
t-– time zone abbreviation-for example: EST,MDT
4
z--time zone difference from the current time zone, in seconds ——-43200 to 43200


Complete Date/time (full date/time)

1
C--iso 8601 Format Date (PHP 5 new)--2004-02-12t15:19:21+00:00
2
R--RFC 822 Format Date--thu, 2000 16:01:07 +0200
3
u--Total description From 1970 1.1 to a certain time, i.e. Unix timestamp, see Time ()


The PHP date function dates formatted characters in English document please refer to http://www.php.net/manual/en/function.date.php

The DATE function uses the hint: when you use the PHP date function to output formatted date format, you can use it handy by first writing out the date format that you want to output, and then using the formatting characters from the date function.

Format a UNIX timestamp by using the date function dates of PHP

PHP Date Date function The second optional parameter, $timestamp, is in the form of a Unix timestamp, which allows the Unix timestamp to be converted to the appropriate date format. The use of Unix timestamps in parameter passing, date calculation and other aspects of the benefits of a lot, convenient and concise.

The usual functions of converting dates into Unix timestamps in PHP are mktime, time, and Strtotime.

The PHP date function mktime is primarily used to convert dates and times into a UNIX timestamp format.

Prototypes are as follows
1
int mktime ([int $hour = Date ("H")
2
[, int $minute = Date ("I")
3
[, int $second = Date ("s")
4
[, int $month = Date ("n")
5
[, int $day = Date ("J")
6
[, int $year = Date ("Y")
7
[, int $is _DST = 1
8
]]]]]]])
Note: All parameters of the Mktime function are optional, where $IS_DST indicates whether it is daylight saving time, and when the Mktime function parameter is empty, it will be assumed to be the current date, similar to the Times function.

The PHP Date function time is primarily used to obtain the current time in UNIX timestamp format.

The PHP date function strtotime is mainly used to convert the date time described in English to a Unix timestamp.

Prototypes are as follows
1
int Strtotime (string $time [, int $now])


Note: $time parameters are mainly in the form of English text description.
$now Optional parameters are mainly expressed as a benchmark for calculating datetime at the time of day, and default to the current UNIX timestamp.

PHP Date function is mainly used to convert the output of various date formats, if you want to get the various parts of the date and time, you can pass the Unix timestamp parameter to the GETDATE function gets, the GETDATE function will return a related array, the array key values are as follows:

01
seconds--Number of seconds
02
minutes--min
03
hours--hours
04
mday--date in month
05
wday--Week, 0 (indicated Sunday) to 6 (for Saturday)
06
mon--Month
07
year--four-digit full year
08
yday--the first day of the year
09
weekday--Week's text representation, Sunday to Saturday
10
month--month text representation, January to December
11
0--the number of seconds since the beginning of the Unix era, similar to the return value of time ().


Through the above several PHP unix timestamp functions, and with the PHP date Date function can be a variety of date format conversion, calculation and output.

PHP Date Calculation Example:

Calculate age

01
02
$day = 1;
03
$month = 1;
04
$year = 1989;
05

06
$birthday = Mktime (0,0,0, $month, $day, $year);
07

08
$nowunix = time ();
09

10
$age = $nowunix-$birthday;
11

12
$age = Floor ($age/(365*24*60*60));
13

14
Echo $age;
15
?>


Date plus minus

01
02
$nowUnix = time ();
03

04
$addTime = $nowUnix + (24*60*60); Date plus Day
05

06
$redTime = $nowUnix-(24*60*60);//date minus Day
07

08
echo Date ("Y-m-j", $addTime);
09
echo Date ("Y-m-j", $redTime);
10
?>


Strtotime Date Calculation and format conversion instance

Date plus or minus can also be done by the Strtotime function



01
02
$nowTime = Strtotime ("Now");
03
echo Date ("Y-m-j", $nowTime). "
";
04

05
$nowTime = Strtotime ("September 2010");
06
echo Date ("Y-m-j", $nowTime). "
";
07

08
$nowTime = Strtotime ("+1 Day");
09
echo Date ("Y-m-j", $nowTime). "
";
10

11
$nowTime = Strtotime ("+1 Week");
12
echo Date ("Y-m-j", $nowTime). "
";
13

14
$nowTime = Strtotime ("+1 Week 2 days 4 hours 2 seconds");
15
echo Date ("Y-m-j", $nowTime). "
";
16

17
$nowTime = Strtotime ("next Thursday");
18
echo Date ("Y-m-j", $nowTime). "
";
19

20
$nowTime = Strtotime ("Last Monday");
21st
echo Date ("Y-m-j", $nowTime). "
";
22
?>
At this point, the PHP date function dates format character meaning and the introduction of some PHP Unix timestamp function and the conversion between each other and formatted output instance is finished, which is necessary to master the calculation of PHP date, time function, and conversion. I hope you will be helpful to a friend who has started learning PHP.

http://www.bkjia.com/PHPjc/478412.html www.bkjia.com true http://www.bkjia.com/PHPjc/478412.html techarticle Web site development We often need to deal with the date, time, in PHP provides a lot of date, time function to facilitate the PHP developers to calculate the date, time, format conversion. So ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.