Asked  8 Months ago    Answers:  5   Viewed   32 times

This was working fine yesterday with no changes to the code.

echo date("M", strtotime("-3 month", time()) );
echo date("M", strtotime("-2 month", time()) );
echo date("M", strtotime("-1 month", time()) );
echo date("M", time());

The output it was producing yesterday was as you would expect- i.e. Apr, May, Jun, Jul

Today it echoes May May Jul Jul

Any ideas?

Thanks in advance.

 Answers

21

It might be related to bug #44073

You could try with something like this :

echo date("M", strtotime("-3 month", strtotime(date("F") . "1")) ) . "n";
echo date("M", strtotime("-2 month", strtotime(date("F") . "1")) ) . "n";
echo date("M", strtotime("-1 month", strtotime(date("F") . "1")) ) . "n";
echo date("M", time()) . "n";

(Solution found in the comments section of strtotime ; direct link)

And the output :

Apr
May
Jun
Jul

Kind of "cheating" with the date format and month's name and all that...

Wednesday, March 31, 2021
 
Valdas
answered 8 Months ago
28

How can I make it return today's date in that case?

pseudocode:

if (today == monday)
    return today;
else
    return strtotime(...);

Btw, this trick also could work:

strtotime('last monday', strtotime('tomorrow'));
Wednesday, March 31, 2021
 
Terry
answered 8 Months ago
67

As @Evan Mulawski's comment says, the "2001" is being interpreted as the time, not the year. Take out the comma to get PHP to interpret the "2001" as a year:

<?php
$ts = strtotime("1 Nov 2001");
echo $ts . "n";
$st = strftime("%B %d, %Y, %H:%M:%S", $ts);
echo $st . "n";
?>

Output:

1004590800
November 01, 2001, 00:00:00

Wednesday, March 31, 2021
 
Trott
answered 8 Months ago
62

From the manual:

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.

Friday, May 28, 2021
 
McAn
answered 5 Months ago
93

This might be useful for people coming here.

/**
 * Format a timestamp to display its age (5 days ago, in 3 days, etc.).
 *
 * @param   int     $timestamp
 * @param   int     $now
 * @return  string
 */
function timetostr($timestamp, $now = null) {
    $age = ($now ?: time()) - $timestamp;
    $future = ($age < 0);
    $age = abs($age);

    $age = (int)($age / 60);        // minutes ago
    if ($age == 0) return $future ? "momentarily" : "just now";

    $scales = [
        ["minute", "minutes", 60],
        ["hour", "hours", 24],
        ["day", "days", 7],
        ["week", "weeks", 4.348214286],     // average with leap year every 4 years
        ["month", "months", 12],
        ["year", "years", 10],
        ["decade", "decades", 10],
        ["century", "centuries", 1000],
        ["millenium", "millenia", PHP_INT_MAX]
    ];

    foreach ($scales as list($singular, $plural, $factor)) {
        if ($age == 0)
            return $future
                ? "in less than 1 $singular"
                : "less than 1 $singular ago";
        if ($age == 1)
            return $future
                ? "in 1 $singular"
                : "1 $singular ago";
        if ($age < $factor)
            return $future
                ? "in $age $plural"
                : "$age $plural ago";
        $age = (int)($age / $factor);
    }
}
Saturday, May 29, 2021
 
EurekA
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 :