Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP date_sub() Function.
The built-in PHP date_sub() function is an alias of DateTime::sub(). This PHP function accepts DateTime and DateInterval object as parameters, and subtracts the specified intetval to the given DateTime.
The built-in PHP date_sub() function is an alias of DateTime::sub(). This PHP function accepts DateTime and DateInterval object as parameters, and subtracts the specified intetval to the given DateTime.
Syntax
Following below is the syntax to use this function -
date_sub($object, $interval)
READ: PHP | date() Function
Parameter Details
Sr.No | Parameter & Description |
---|---|
1 | object(Mandatory) This is a DateTime object specifying/representing the date from which you need to subtract the time interval. |
2 | interval (Mandatory) This is a DateInterval object specifying the interval to be subtracted. |
Return Value
This function returns a DateTime object with subtracted interval. In case of failure it returns false.
PHP Version
This function was first introduced as part of core PHP version 5.3.0 and, it works with all of the later versions.
Example1
Following example illustrates the usage of date_sub() function -
<?php //Creating a DateTime object $date = date_create("25-09-2019"); //Adding interval to the date $res = date_sub($date, new DateInterval('PT10H30S')); //formatting the date to print it $format = date_format( $res, "d-m-Y H:i:s"); print($format); ?>
Output
When the above code is executed, it will produce the following result -
24-09-2019 13:59:30
Example2
The following example creates an interval using this function and, subtracts it from a date -
<?php $date = date_create("25-09-1989"); $interval = date_interval_create_from_date_string('1025 days'); $res = date_sub($date, $interval); $format = date_format( $res, "d-m-Y"); print($format); ?>
Output
When the above code is executed, it will produce the following result -
05-12-1986
Example3
Now let us try to add interval with years, months, and days -
<?php //Creating a DateTime object $date = date_create("25-09-1989"); //Adding interval to the date $res = date_sub($date, new DateInterval('P29Y2M5D')); //formatting the date to print it $format = date_format( $res, "d-m-Y"); print($format); ?>
Output
When the above code is executed, it will produce the following result -
20-07-1960
Example4
Try the following example -
<?php $date = date_create('1995-05-07'); $interval = date_interval_create_from_date_string('150 days'); $date->sub($interval); print($date -> format('d-m-Y')); ?>
Output
When the above code is executed, it will produce the following result -
08-12-1994
READ: PHP | time() Function
Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the date_timestamp_get() Function in PHP.
Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.
Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.