For others who might be reading, what was the answer you found?
Well, I am still using DateTime instead just Date (this is for today date ). I was concerned my [ânextmaintâ] would need hours and minutes to work but it works fine with date only.
So I went from this:
<?php
$v = $row_rsHardwareAsset['maintenanceint'];
$date = $row_rsHardwareAsset['lastmaint'];
$date = strtotime(date("Y-m-d", strtotime($date)) . " +$v month");
$date = date("Y-m-d", $date); // next maintenance day
$now = new DateTime();
$someDateDatetime = new DateTime($date);
$diff = $someDateDatetime->diff($now);
if ($someDateDatetime < $now) {
echo 'MAINTENANCE OVERDUE</br>
<span style="color:red;text-align:center;">by ' . $diff->m . ' months, ' . $diff->d . ' days</span>';
} else {
$diff = $someDateDatetime->diff($now);
echo '' . $diff->m . ' months, ' . $diff->d . ' days</br>';
}
?>
To this:
<?php
$date = $row_rsHardwareAsset['nextmaint'];
$date = strtotime(date("Y-m-d", strtotime($date)));
$date = date("Y-m-d", $date);
$now = new DateTime();
$someDateDatetime = new DateTime($date);
$diff = $someDateDatetime->diff($now);
if ($someDateDatetime < $now) {
echo 'MAINTENANCE OVERDUE</br>
<span style="color:red;text-align:center;">by ' . $diff->m . ' months, ' . $diff->d . ' days</span>';
} else {
$diff = $someDateDatetime->diff($now);
echo '' . $diff->m . ' months, ' . $diff->d . ' days</br>';
}
?>
if nextmaint is a DATE field, you donât need the date-reconfiguring (lines 3 & 4), just put it into the DateTime constructor as is. You also might consider using new DateTime('today') as that would get rid of the time part.
nextmaint for example looks like: 2018-09-15
I thought I need to turn date into timestamp (as far as I know strtotime does this function)
All the more reason to drop those two lines.
$someDateDatetime = new DateTime(date("Y-m-d", strtotime(date("Y-m-d", strtotime('2018-09-15')))));
looks overboarding, doesnât it?
Yes, it does.
I have removed two lines as you said and code works fine. Any more refinements?
<?php
$date = $row_rsHardwareAsset['nextmaint'];
$now = new DateTime();
$next = new DateTime($date);
$diff = $next->diff($now);
if ($next < $now) {
echo 'MAINTENANCE OVERDUE</br>
<span style="color:red;text-align:center;">by ' . $diff->m . ' months, ' . $diff->d . ' days</span>';
} else {
$diff = $next->diff($now);
echo '' . $diff->m . ' months, ' . $diff->d . ' days</br>';
}
?>
you can remove the date diff inside else, since the date diff already exists.
But then it wonât give me number of days itâs overdue? As I want to know how many days past the nextmaint has gone
EDIT: I see your point now. There is two exactly same lines so donât need it twiceâŚ
I also moved $row_rsHardwareAsset[ânextmaintâ] inside DateTime constructor.
<?php
$now = new DateTime();
$next = new DateTime($row_rsHardwareAsset['nextmaint']);
$diff = $next->diff($now);
if ($next < $now) {
echo 'MAINTENANCE OVERDUE</br>
<span style="color:red;text-align:center;">by ' . $diff->m . ' months, ' . $diff->d . ' days</span>';
} else {
echo '' . $diff->m . ' months, ' . $diff->d . ' days</br>';
}
?>
A post was split to a new topic: How to insert data into DB which would be altered by selected number
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.