Skip to content

Instantly share code, notes, and snippets.

@MasterHans
Created October 21, 2016 09:59
Show Gist options
  • Save MasterHans/41d4f197c385be4f3477d2d52245dd64 to your computer and use it in GitHub Desktop.
Save MasterHans/41d4f197c385be4f3477d2d52245dd64 to your computer and use it in GitHub Desktop.
<?php
$raw = '22. 11. 1968';
$start = DateTime::createFromFormat('d. m. Y', $raw);
echo 'Start date: ' . $start->format('Y-m-d') . "\n";
<?php
// create a copy of $start and add one month and 6 days
$end = clone $start;
$end->add(new DateInterval('P1M6D'));
$diff = $end->diff($start);
echo 'Difference: ' . $diff->format('%m month, %d days (total: %a days)') . "\n";
// Difference: 1 month, 6 days (total: 37 days)
<?php
if ($start < $end) {
echo "Start is before end!\n";
}
<?php
// output all thursdays between $start and $end
$periodInterval = DateInterval::createFromDateString('first thursday');
$periodIterator = new DatePeriod($start, $periodInterval, $end, DatePeriod::EXCLUDE_START_DATE);
foreach ($periodIterator as $date) {
// output each date in the period
echo $date->format('Y-m-d') . ' ';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment