Asked  9 Months ago    Answers:  5   Viewed   143 times

I'm using the following code:

public static function getDays($day, $m, $y) {
  $days = new DatePeriod(
    new DateTime("first $day of $m $y"),
    DateInterval::createFromDateString('next ' . $day),
    new DateTime("last day of $m $y")
  foreach ($days as $day) {
    $dates[] = $day->format("Y-m-d");
  return $dates;

It work's for the most part. I give it a range and it returns the date of each day I am after inside the range.


foreach(General::getDays('friday',$this->_uri[2],$this->_uri[3]) as $date) {

In July 2013, this returns the correct 4 results, all friday's in July 2013:

string '2013-07-05' (length=10)
string '2013-07-12' (length=10)
string '2013-07-19' (length=10)
string '2013-07-26' (length=10)

However, on a month that the last day is a match to the day occurance I am after (friday for example, May 2013 and January 2014) it misses the last day of the month out of the returned results.

May 2013 results, missing of course the 31st which is a friday:

string '2013-05-03' (length=10)
string '2013-05-10' (length=10)
string '2013-05-17' (length=10)
string '2013-05-24' (length=10)

Does anyone have an answer for this? Is this my miss-use of DateInterval or a genuine fault in this class?



The problem is that the end date/time itself is never included in the period -- that is, the DatePeriod object represents date/times in the [$startDate, $endDate) range┬╣.

You have to add one second at least to the end date to always get the expected results. But let's not be stingy and add a whole minute:

$days = new DatePeriod(
    new DateTime("first $day of $m $y"),
    DateInterval::createFromDateString('next ' . $day),
    new DateTime("last day of $m $y 00:01")

┬╣ unless you use the DatePeriod::EXCLUDE_START_DATE option, in which case the start date itself is excluded as well

Wednesday, March 31, 2021
answered 9 Months ago

You can compare the day of the month before and after you add 1 month. If it's not the same, you exceeded the next month.

function add($date_str, $months)
    $date = new DateTime($date_str);

    // We extract the day of the month as $start_day
    $start_day = $date->format('j');

    // We add 1 month to the given date
    $date->modify("+{$months} month");

    // We extract the day of the month again so we can compare
    $end_day = $date->format('j');

    if ($start_day != $end_day)
        // The day of the month isn't the same anymore, so we correct the date
        $date->modify('last day of last month');

    return $date;

$result = add('2011-01-28', 1);   // 2011-02-28
$result = add('2011-01-31', 3);   // 2011-04-30
$result = add('2011-01-30', 13);  // 2012-02-29
$result = add('2011-10-31', 1);   // 2011-11-30
$result = add('2011-12-30', 1);   // 2011-02-28
Wednesday, March 31, 2021
answered 9 Months ago

In PHP 5.3, you can use the DateTime class :


$month_ini = new DateTime("first day of last month");
$month_end = new DateTime("last day of last month");

echo $month_ini->format('Y-m-d'); // 2012-02-01
echo $month_end->format('Y-m-d'); // 2012-02-29
Wednesday, March 31, 2021
answered 9 Months ago

A note from DateInterval::format:

The DateInterval::format() method does not recalculate carry over points in time strings nor in date segments. This is expected because it is not possible to overflow values like "32 days" which could be interpreted as anything from "1 month and 4 days" to "1 month and 1 day".

So one has to recalculate carry over points. Below is the relevant code from DateInterval::format:

class DateIntervalEnhanced extends DateInterval {
    public function recalculate() {
        $from = new DateTime;
        $to = clone $from;
        $diff = $from->diff($to);
        foreach ($diff as $k => $v) $this->$k = $v;
        return $this;

A utility function:

function myFormatter($d1, $d2, $format) {
    $diff = strtotime($d1) - strtotime($d2);
    $df = abs($diff);
    $di = new DateIntervalEnhanced("PT${df}S");
    $di->invert = $diff < 0;
    return $di->recalculate()->format($format);

echo myFormatter("2017-12-31 23:59:59", "2017-12-01 00:00:00", "%m");


Link to a post you might wanna read

Saturday, May 29, 2021
answered 7 Months ago

You need to call DateInterval::format() to display that difference as a string.

echo $diff->format('%d days');

See the manual for all of the available formatting options.

Wednesday, July 7, 2021
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :