SoFunction
Updated on 2025-03-10

Examples of common functions used in PHP time-related

This article describes the usage of common functions related to PHP time. Share it for your reference, as follows:

<?php
/**
  * Set the time zone
  */
date_default_timezone_set("Asia/Shanghai");
 
/**
  * Get the time zone
  */
echo date_default_timezone_get();
//Result UTCecho "<br/>";
 
/**
  * Add time
  */
$date=date_create("2013-03-15"); //Create a DateTime objectdate_add($date,date_interval_create_from_date_string("40 month"));//years days
//date_interval_create_from_date_string Create a DateInterval from the relevant part of the string.echo date_format($date,"Y-m-d");
//Result 2016-07-15echo "<br/>";
 
/**
  * Subtract time
  */
$date=date_create("2013-03-15");
date_sub($date,date_interval_create_from_date_string("40 days"));
echo date_format($date,"Y-m-d");
//2013-02-03
echo "<br/>";
 
/**
  * Get the difference between two time zones
  */
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);//Return is a DateInterval objectecho "<pre>";
var_dump($diff);
// object(DateInterval)#4 (15) {
//  ["y"]=>
//  int(0)
//  ["m"]=>
//  int(8)
//  ["d"]=>
//  int(27)
//  ["h"]=>
//  int(0)
//  ["i"]=>
//  int(0)
//  ["s"]=>
//  int(0)
//  ["weekday"]=>
//  int(0)
//  ["weekday_behavior"]=>
//  int(0)
//  ["first_last_day_of"]=>
//  int(0)
//  ["invert"]=>
//  int(0)
//  ["days"]=>
//  int(272)
//  ["special_type"]=>
//  int(0)
//  ["special_amount"]=>
//  int(0)
//  ["have_weekday_relative"]=>
//  int(0)
//  ["have_special_relative"]=>
//  int(0)
// }
echo "<br/>";
 
/**
  * Get the current time stamp
  */
$date=date_create();
echo date_timestamp_get($date) .'<br/>';
 
$time = time();
echo $time .'<br>';
 
echo strtotime("now") .'<br/>';
 
/*
  * Get the 0-point time stamp today
  */
 
echo strtotime("today").'<br>';
/**
  * Get time with microseconds
  */
echo microtime(true);
 
/*
  * Get the specified timestamp
  * mktime(hour, minute, second, month, day, year);
  */
echo "<br/>";
echo mktime(18,30,15,3,15,2019);
 
/*
  * Get the 0-point time stamp of the previous day
  */
echo "<br/>";
echo strtotime('yesterday');
 
/*
  * Get the time stamp from yesterday
  */
echo "<br/>";
 
echo strtotime('-1 days');
 
?>

Running results:

Asia/Shanghai
2016-07-15
2013-02-03

object(DateInterval)#4 (15) {
  ["y"]=>
  int(0)
  ["m"]=>
  int(8)
  ["d"]=>
  int(27)
  ["h"]=>
  int(0)
  ["i"]=>
  int(0)
  ["s"]=>
  int(0)
  ["weekday"]=>
  int(0)
  ["weekday_behavior"]=>
  int(0)
  ["first_last_day_of"]=>
  int(0)
  ["invert"]=>
  int(0)
  ["days"]=>
  int(272)
  ["special_type"]=>
  int(0)
  ["special_amount"]=>
  int(0)
  ["have_weekday_relative"]=>
  int(0)
  ["have_special_relative"]=>
  int(0)
}
1591150859
1591150859
1591150859
1591113600
1591150859.0074
1552645815
1591027200
1591064459

PS: Here are a few time and date related tools for your reference:

Online Date/Day Calculator:
http://tools./jisuanqi/date_jisuanqi

Online date calculator/phase difference day calculator:
http://tools./jisuanqi/datecalc

Online date day difference calculator:
http://tools./jisuanqi/onlinedatejsq

Unix timestamp conversion tool:
http://tools./code/unixtime

For more information about PHP related content, please check out the topic of this site:Summary of the usage of php date and time》、《Complete collection of PHP array (Array) operation techniques》、《Introduction to PHP basic syntax》、《Summary of PHP operations and operator usage》、《PHP object-oriented programming tutorial》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope this article will be helpful to everyone's PHP programming.