Viewed   75 times

I want to use strtotime("last Monday").

The thing is, if today IS MONDAY, what does it return? It seems to be returning the date for the monday of last week. How can I make it return today's date in that case?

 Answers

1

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'));
Saturday, December 24, 2022
2

strtotime doesn't "output" anything, btw : it returns false in case of an error ; see the manual :

Return Values

Returns a timestamp on success, FALSE otherwise. Previous to PHP 5.1.0, this function would return -1 on failure.

What doesn't output anything is echo : false is considered as an empty string, and nothing get outputed.

strtotime's documentation also gives the valid range for dates :

Note: The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.) Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems. PHP 5.1.0 and newer versions overcome this limitation though.

'0000-00-00' is outside of this range, so it's not considered a valid date ; hence the false return value.


As a sidenote, to really know what's inside a variable, you can use var_dump.
As a bnus, used with Xdebug, it'll get you a nice-formated output ;-)

Sunday, August 7, 2022
2

Looks like you are just missing an "of":

echo date('Y-m-d', strtotime('first monday of february 2010'));

will give the expected result. See the PHP Manual on Relative dates for the various input formats.

Saturday, September 3, 2022
 
2

If you have PHP 5.3:

$date = DateTime::createFromFormat('d/m/Y H:i:s', '03/05/2011 16:33:00');
echo $date->getTimestamp();
Monday, September 5, 2022
 
5

Actually, you don't need timestamp variable because:

Exerpt from date function of php.net:

Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time().

if(date('j', $timestamp) === '1') 
    echo "It is the first day of the month todayn";

if(date('D', $timestamp) === 'Mon') 
    echo "It is Monday todayn";
Sunday, December 25, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :