InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Adding to or Subtracting from a Date in PHP
PHP

Adding to or Subtracting from a Date in PHP

InfinityCoder November 18, 2016

You need to add or subtract an interval from a date.

Apply a DateInterval object to a DateTime object with either the DateTime::add() or DateTime::sub() method, as shown in Example 3-21.

Example 3-21. Adding and subtracting a date interval

1
2
3
4
5
6
7
8
9
10
11
12
$birthday = new DateTime('March 10, 1975');
 
// When is 40 weeks before $birthday?
$human_gestation = new DateInterval('P40W');
$birthday->sub($human_gestation);
print $birthday->format(DateTime::RFC850);
print "\n";
 
// What if it was an elephant, not a human?
$elephant_gestation = new DateInterval('P616D');
$birthday->add($elephant_gestation);
print $birthday->format(DateTime::RFC850);

The add() and sub() methods of DateTime modify the DateTime method they are called on by whatever amount is specified in the interval.

The average human gestation time is 40 weeks, so an interval of P40W walks back the birthday to 40 weeks earlier, approximating conception time.

An elephant, on the other hand, has an average gestation time of 616 days.

So, adding an interval of P616D to that conception time produces the expected due date of an elephant conceived at the same time as the human.

A DateTime object’s modify() method accepts, instead of a DateInterval object, a string that strtotime() understands.

This provides an easy way to find relative dates like “next Tuesday” from a given object. For example, election day in the United States is the Tuesday after the first Monday in November.

(That is, the first Tuesday of November, unless that’s the first of the month, in which case it’s the following Tuesday.) With DateTime::modify() you can find the date of election day as follows:

1
2
3
4
5
6
7
8
9
$year = 2016;
$when = new DateTime("November 1, $year");
if ($when->format('D') != 'Mon') {
    $when->modify("next Monday");
}
$when->modify("next Tuesday");
 
print "In $year, US election day is on the " .
    $when->format('jS') . ' day of November.';

The format character D produces the day of the week. So if the first day of November is not a Monday, the call to $when->modify(“next Monday”) advances the DateTime object to the following Monday.

Then, the subsequent call to modify() finds the first Tuesday after that.

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Getting and Setting File Timestamps in PHP
You want to know when a file was last accessed …

Getting and Setting File Timestamps in PHP

Reading from Standard Input in PHP
You want to read from standard input in a command-line …

Reading from Standard Input in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap