I want to check if today is the last day of the month, but I don't really know how.
Can you help?
I want to check if today is the last day of the month, but I don't really know how.
Can you help?
There is probably a more elegant solution than this but you can just use php's date function:
$maxDays=date('t');
$currentDayOfMonth=date('j');
if($maxDays == $currentDayOfMonth){
//Last day of month
}else{
//Not last day of the month
}
time()
calls are redundant since the current timestamp is what's used by default. Not that it matters though :) –
Villager date('t');
or you may use
cal_days_in_month.
see here: http://php.net/manual/en/function.cal-days-in-month.php
In order to get no. of days in month you can use either
date('t')
OR
cal_days_in_month(CAL_GREGORIAN, 8, 2003)
And if you wish to check if today is the last day of month us can use
if(date('d')==date('d',strtotime('last day of month'))){
//your code
}
strtotime offers many great features so check them out first
Use date function:
if (date('t') == date('j'))
{
...
}
$date = new DateTime('last day of this month');
$numDaysOfCurrentMonth = $date->format('d');
this is to find the days in any month:
$days = intval(date('t', strtotime($desiredDate)));
echo date('t'); /// it return last date of current month
And here it is, wrapped up as a function:
function is_last_day_of_month($timestamp = NULL) {
if(is_null($timestamp))
$timestamp = time();
return date('t', $timestamp) == date('j', $timestamp);
}
Using the modern DateTime object class:
$date = new DateTime('last day of this month');
With DateTime ;)
$date = new \DateTime('now');
var_dump($date->format('t'));
© 2022 - 2024 — McMap. All rights reserved.