Skip to content

Instantly share code, notes, and snippets.

@andrewwoods
Last active October 19, 2017 01:06
Show Gist options
  • Save andrewwoods/23387dfac2db5b58aa5a96946a5c1f74 to your computer and use it in GitHub Desktop.
Save andrewwoods/23387dfac2db5b58aa5a96946a5c1f74 to your computer and use it in GitHub Desktop.
Add Date Intervals with PHP
<!-- GATHER INFORMATION FROM FORM -->
<!-- Get date due, the Friday before the week for which the taxi fare applies -->
<?php
/*
* Create date for testing (due date Friday, 10-13-17)
*/
$monthDue = 10; // $_POST['monthdue'];
$dayDue = 13; // $_POST['daydue'];
$yearDue = 2017; // $_POST['yeardue'];
/*
* Setup date intervals
*/
$oneDay = new DateInterval('P1D');
$sevenDays = new DateInterval('P7D');
/*
* Convert date to digital format yyyy-mm-dd
* Prepend single digit values with zero
*/
if ($monthDue <= 9) {
$monthDue = 0 . $monthDue;
}
if ($dayDue <= 9) {
$dayDue = 0 . $dayDue;
}
$dateDueFormatted = $yearDue . '-' . $monthDue . '-' . $dayDue;
$dateDue = date_create($dateDueFormatted);
echo "Due date (\$date): " . $dateDue->format('Y-m-d');
?>
<br/>
<!-- Add one day to due date (Saturday date, start of taxi fare week) -->
<?php
$dateStart = $dateDue->add($oneDay);
echo "Start date: " . $dateStart->format('Y-m-d');
?>
<br/>
<!-- Add seven days to due date (Next Friday's date, End of taxi fare week) -->
<?php
$dateEnd = $dateStart->add($sevenDays);
echo "End date: " . $dateEnd->format('Y-m-d');
?>
<br/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment