Summarizing the common methods of datetime in PHP _php tips

Source: Internet
Author: User
Tags time zones time interval

Objective

The instantiation object is preceded by the use of the native class in the namespace, and if the namespace is not used, the preceding \ can be deleted

1. Output Current time

$datetime = new \datetime;
Print_r ($datetime->format (' y-m-d h:i:s '));

2. Output a given time

$datetime = new \datetime (' 2016-06-13 ');
Print_r ($datetime);

3. According to the given time format to the time you want

$datetime = \datetime::createfromformat (' Ymd ', ' 20160618 ');
Print_r ($datetime->format (' y-m-d '));

4. Output UNIX Timestamp format (Method 1 Returns a negative number if it was before 1990, and Method 2 returns false)

Method 1 (php5.2):
$datetime = new \datetime ();
echo $datetime->format (' U '); exit;

Method 2 (php5.3) Recommended
$datetime = new \datetime ();
echo $datetime->gettimestamp (); exit;

5. Format to a given time according to a given timestamp

$datetime = new \datetime ();
$datetime->settimestamp (1465783744);
echo $datetime->format (' y-m-d h:i:s ');

6. Two date and time comparison, year to year, month to month ratio ...

$datetime 1 = new \datetime (' 2016-01-01 10:11:18 ');
$datetime 2 = new \datetime (' 2017-05-11 22:21:21 ');
$interval = $datetime 1->diff ($datetime 2);
Print_r ($interval->format ('%Y '));//% indicates that the use of formatting, r means is greater than this date (+), or less than this date (-), a is greater than or less than the number of days, when the normal use of minutes and seconds y,m,d,h,i,s

7. Create a length of time before a few days

The parameter of the DateInterval constructor is a string representing the time interval convention, which begins with the letter p, followed by an integer, and finally a periodic identifier that qualifies the preceding integer. The valid period identifier is as follows: Y (year) m (month) D (days) W (week) H (time) m (min) S (SEC) Interval convention can have time or date, if you have time to add the letter T between the date and time, for example, the interval convention p2d the interval of two days, The interval agreement p2dt5h2m represents an interval of two days five hours and two minutes.

$datetime = new \datetime ();
$interval = new \dateinterval (' p2dt5h ');
Or use the Createfromdatestring method
//$interval = \dateinterval::createfromdatestring (' 1 month ');
Modify DateTime instance
$datetime->add ($interval);
echo $datetime->format (' y-m-d h:i:s ');

8. Create a few days ago

$datetime = new \datetime ();
$interval = new \dateinterval (' p2dt5h ');
$datetime->sub ($interval);
echo $datetime->format (' y-m-d h:i:s ');
PS: There is a modify method, this method is minus 30, not as forward 1 days, output or December
$datetime = new \datetime (' 2014/12/31 ');
$datetime->modify ('-1 month ');
Print_r ($datetime); exit;

9. Resets the current DateTime object to a different date, passing year, month, day

$datetime = new \datetime ();
$datetime->setdate (2015, 2);
echo $datetime->format (' y-m-d '); exit;

10. Reset the time of the current DateTime object at different times, when passing, minutes, seconds (optional parameters)

$datetime = new \datetime ();
$datetime->settime (a);
echo $datetime->format (' y-m-d h:i:s ');

11. Time zone for changing times before formatting time

$timezone = new \datetimezone (' Asia/calcutta ');
$datetime = new \datetime ();
$datetime->settimezone ($timezone);
Print_r ($datetime->format (' y-m-d h:i:s '); exit;

12. Return time zone

$date = new \datetime (null, New Datetimezone (' Asia/shanghai '));
$tz = $date->gettimezone ();
echo $tz->getname ();

13. Calculate offset values for two time zones

$dateTimeZoneTaipei = new \datetimezone ("Asia/taipei");
$dateTimeZoneJapan = new \datetimezone ("Asia/tokyo");
$dateTimeTaipei = new \datetime ("Now", $dateTimeZoneTaipei);
$dateTimeJapan = new \datetime ("Now", $dateTimeZoneJapan);
$timeOffset = $dateTimeZoneJapan->getoffset ($dateTimeTaipei);
Print_r ($timeOffset); exit;

14. Return time interval, how long time

$interval = new \dateinterval (' p2y4dt6h8m ');
echo $interval->format ('%d days ');

15. The iteration output is the date of the day before the current date.

The construction method of the Dateperiod class accepts three parameters and must provide a DateTime instance that represents the date and time of the beginning of the iteration, an DateInterval instance representing the interval of the next date and time, an integer representing the total number of iterations the fourth parameter is optional, is used to explicitly specify the end date and time of the cycle, and if you want to exclude the start date and time from the iteration, you can set the last parameter of the constructor to a DatePeriod::EXCLUDE_START_DATE constant:

$datetime = new \datetime ();
$interval = \dateinterval::createfromdatestring ('-1 day ');
$period = new \dateperiod ($datetime, $interval, 3);
foreach ($period as $date) {
  echo $date->format (' y-m-d '), Php_eol;
}

Summarize

The above is the common method of DateTime in PHP summary of the whole content, the content is complete, I believe that the daily use of PHP is very helpful, we recommend that the collection for easy access.

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.