Knowledge Base Ever

PHP date addition and subtraction method

2019-11-21 11:05:49

label: php,date add,date sub, Time addition and subtraction

method one

echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
echo strtotime("2019-01-01 12:12:12 +1 day"), "\n";

Method Two

$second = 3600;
$timestamp = strtotime("now") + $second;

Convert timestamp to string format

date ( string $format [, int $timestamp ] ) : string

// 2001-03-10 17:16:18
echo date("Y-m-d H:i:s");

//Monday 15th of August 2005 03:12:46 PM
echo date('l dS \of F Y h:i:s A');
d: the first few days of the month, with 2 digits leading zero, 01 to 31
D: The day of the week, the text indicates that 3 letters, Mon to Sun
j: the first few days of the month, no leading zeros, 1 to 31
l: day of the week, full text format, Sunday to Saturday
N: ISO-8601 format number indicates the day of the week (PHP 5.1.0 new), 1 (for Monday) to 7 (for Sunday)
S: English suffix after the number of days per month, 2 characters, st, nd, rd or th. Can be used with j
w: the day of the week, the number indicates 0 (for Sunday) to 6 (for Saturday)
z: the first few days of the year, 0 to 365
W: The first few weeks of the ISO-8601 format year, starting on Monday every week (new in PHP 4.1.0), for example: 42 (42nd week of the current year)
F: month, full text format, such as January or March, January to December
m: the month indicated by the number, with leading zeros, 01 to 12
M: the month represented by the three-letter abbreviation, Jan to Dec
n: the month represented by the number, without leading zeros, 1 to 12
t: a few days in the specified month, 28 to 31
L: Is it a leap year, if it is a leap year, it is 0 otherwise
o: ISO-8601 format year number. This is the same as the value of Y except that if the number of weeks (W) of the ISO belongs to the previous year or the next year, then that year is used. (PHP 5.1.0 new), Examples: 1999 or 2003
Y: year in which the 4-digit number is fully represented, for example: 1999 or 2003
y: the year represented by 2 digits, for example: 99 or 03
a: lower morning and afternoon values, am or pm
A: Uppercase morning and afternoon values, AM or PM
B: Swatch Internet standard, 000 to 999
g: hour, 12 hour format, no leading zeros, 1 to 12
G: hour, 24-hour format, no leading zeros, 0 to 23
h: hour, 12 hour format, with leading zeros, 01 to 12
H: hour, 24-hour format, with leading zeros, 00 to 23
i: minutes with leading zeros, 00 to 59
s: seconds, with leading zeros, 00 to 59
u: milliseconds (new in PHP 5.2.2). Note that the date() function always returns 000000 because it only accepts the integer argument, while DateTime::format() only supports milliseconds. Example: 654321
e: time zone identifier (new in PHP 5.1.0), for example: UTC, GMT, Atlantic/Azores
I: Is it daylight saving time? If it is daylight saving time, it is 0 otherwise 0.
O: The number of hours that differ from Greenwich Mean Time, for example: +0200
P: Difference from Greenwich Mean Time (GMT), there is a colon between hours and minutes (new in PHP 5.1.3), for example: +02:00
T: The time zone in which the unit is located, for example: EST, MDT ([Translator's Note] is a full text format under Windows, such as "Eastern Standard Time", Chinese version will display "China Standard Time").
Z: The number of seconds of the time difference offset. The time zone offset to the west of UTC is always negative, and the time zone offset to the east of UTC is always positive. -43200 to 43200
c: date in ISO 8601 format (new to PHP 5), 2004-02-12T15:19:21+00:00
r: date in RFC 822 format, for example: Thu, 21 Dec 2000 16:01:07 +0200
U: Seconds since the Unix era (January 1 1970 00:00:00 GMT)


CentOS7 CentOS PHP Linux Python3 Python MySQL YUM JavaScript MariaDB Redis MySQL8 caching_sha2_password HTML mysql_native_password PHP7 CSS SOCKS Web Development data INDENTIFIED BY password policy requirements SVN Fedora Media Writer msodbcsql Redis5 SQLAlchemy absolute Deepin jemalloc Shadowsocks toggle convert FuelPHP mysqlclient ss5.passwd at-symbol Error Control Operator margin PHPUnit Framework SOCKET vps daemon process IIS password sudoers center Fatal Errors MongoDB4 SQL Server 2017 WiFi .htaccess DATE_SUB Phalcon set_error_handler TestCase Compiling PHP frequency conflict ss5.conf apr Elasticsearch7.3 PHPUnit Slim Vincent Garreau daemon HTTP particles.js Sudo Failed to build these modules: _uuid MongoDB SQL Server width DATE_ADD ip command pg_bha.conf scrollbar Tcp BBR Compiling Apache Frameworks MySQL-python ss5-3.8.9 Apache2.4 Elasticsearch libpython3.7m.so.1.0 PHPStorm show VIM CURLOPT_POSTFIELDS NodeJs substr Bluetooth Failed to build these modules: _ctypes Minimal ISO SQL Date Funcitons Wi-Fi date sub INTERVAL PECL root Tag Cloud CodeIgniter font-size ss5 apache httpd dual network interfaces Laravel ShadowsocksR vertical-align CURLFille horiz-align node.js SUBDATE BarManager exec mb_substr PyCharm SOCKS5 Proxy Server WebStorm date add IntelliJIDEA PEAR Tag Chrome find remi src Apache du jQuery PHP5.5 Shadowsocks-Python V2Ray cURL hide nginx sub folder availability eth0 MariaDB10.4 PostgreSQL SOCKS5 Web Server date Install MongoDB pdo_sqlsrv Symfony File Upload mssql-tools register_shutdown_function sqlsrv Zend Framework ADDDATE DevExpress JetBrain php-fpm Shadowsocks-Libev unixODBC-devel height network interface string Aura error_get_last Port 80