PHP calculating remaining time between two dates

It’s example code to show you how to use DateTime, rather than all the code you need to use it without modification. You can see where the original date is being set into $someDate, and you can use the code you already have to add the maintenance interval time to it before you do the comparison and difference calculations.

Why then make it so complicated and not give a code that should work? That’s all I asked, my skills are limited and I will simply waste a lot of time before it starts to work, is it so difficult to understand this? Why people apart from m_hutley gives no valuable information, just some sort of clues? Be more simple :ok_hand:

Because we don’t do your homework for you. Most or all of the people here are volunteers and not code-factories.

That’s your flaw and the problem you should work on - work on yourself to be more then you are now. People here encourage and help you getting better.

YES. It’s called “learning”. Letting other people do your work will only lead to you coming here over and over again instead of solving your problems on your own.

We did. You want other people to do your work. That’s a legit cause, but most people here just won’t do it, so you may have missed the topic of this forum.

These examples are pretty simple - but the goal is that you learn to solve your problems on your own. You may need a kick now and then, but why should anyone make the hard work for you? Don’t we have other things to do in our free time, and don’t other people - that are willing to learn! - also need help?

3 Likes

You have just written a post which again does not answer topic question.

Because we don’t do your homework for you. Most or all of the people here are volunteers and not code-factories.
It’s not a home work, just a project for myself. I appreciate people are volunteers and I already offered donation for answering topic question with a working reply

That’s your flaw and the problem you should work on - work on yourself to be more then you are now. People here encourage and help you getting better.
I am not programming for living and I simply find a lot easier to learn from examples rather than digging clues. Each for their own.

YES. It’s called “learning”. Letting other people do your work will only lead to you coming here over and over again instead of solving your problems on your own.
At the moment project is a bit urgent as I need to make it work, I learn through projects which are not time tight.

We did. You want other people to do your work. That’s a legit cause, but most people here just won’t do it, so you may have missed the topic of this forum.
You DID NOT. If you would read first post again, you will find request that I need some sort of function, but not “how things work” explanation. It is very obvious that I have no idea where and what function to add to make it do the job needed. ALSO there is genuine people who would just give you answer straight away and I think they are the strong part of such forums.

These examples are pretty simple - but the goal is that you learn to solve your problems on your own. You may need a kick now and then, but why should anyone make the hard work for you? Don’t we have other things to do in our free time, and don’t other people - that are willing to learn! - also need help?
They are simple for YOU because you KNOW how it works. If you would need advice in designing Raychem system structures or developing control POD’s for complex industrial machinery I would be more than happy to give you advice, vice-versa I expect to get solid answers here. And this may be (very) hard work for me but easy-peezy for someone much more advanced than me thus saving A LOT of my time and spending A LITTLE of someone’s else. I also offer donation (as already mentioned above) for SOLUTIONS (actually I think it should cost more to teach rather then just give code which would work, huh?) so from your post I believe you just having a bad day or being too negative.

Where was that? I missed it.

It’s always difficult to tell how much knowledge and experience a poster has - your original code clearly shows that you know how to get a date from the database, add a period of time to it to produce another date, and it’s really simple to then combine that with the code that @TenDolla gave you.

// Your code

$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

// TenDolla 's code

// $someDate = '2018-01-01';  // no longer needed
$now = new DateTime();
$someDateDatetime = new DateTime($date); // use your calculated date instead

if ($someDateDatetime < $now) {
    echo 'someDate is in the past. Maintenance has expired</br>';
} else {
    echo 'someDate is in the future, or the exact same moment.</br>';
}

$diff = $someDateDatetime->diff($now);

echo 'Differenct between someDate and now: </br>';
echo 'Years: ' . $diff->y . '</br>';
echo 'Months: ' . $diff->m . '</br>';
echo 'Days: ' . $diff->d . '</br>';
echo 'Hours: ' . $diff->h . '</br>';
echo 'Minutes: ' . $diff->i . '</br>';
echo 'Seconds: ' . $diff->s . '</br>';
echo 'Total days: ' . $diff->days;

If I get decent support always send a message to user offering donation. In such way trying to avoid commercialism. It should start with goodwill, I think.

This code I have posted is modified sample from other forum.

I know how to get today’s date and I have managed involve $v as an maintenance interval value.

Will try it and report.

Off Topic:

Any such offer would be removed by the moderators. This is a place for discussion, not a paid coding service, so offers of remuneration are not appropriate.

5 Likes

There seems to be some contradictions.

I learn best from example code - yet it appears that the example code is not thoroughly understood.

I am not programming for a living - project is a bit urgent.

In any case, reading documentation and looking at example code is fine, but IMHO it only goes so far and nothing beats writing actual trial and error code to see how things work. In fact, I would say I’ve learned more from my mistakes than any amount of reading documentation or looking at example code.

IMHO, working with a Date can be confusing. What with the different RFC formats and different ways a Date object can be created, knowing how to go about doing what is wanted isn’t always so easy.

If you want to learn, I suggest you start out with simple code and once you feel comfortable with that incrementally add complexity.

The online PHP documentation often has both examples and user contributed examples that can be very helpful. Take a look at the various functions and when you see something that looks like it might be something you want to use try some of the examples. As they are at first, then try changing argument values to get a grasp of what they are doing.

http://php.net/manual/en/function.date.php

eg.
http://php.net/manual/en/dateinterval.createfromdatestring.php

$i = DateInterval::createFromDateString('3 months');
var_dump($i); 

or the examples at
http://php.net/manual/en/datetime.diff.php

2 Likes

So what’s the actual difference between

$i = DateInterval::createFromDateString('3 months');
var_dump($i);

and

$v = $row_rsHardwareAsset['maintenanceint'];

?

The first is an object, the second a scalar (string or integer, depending on the source data and some connection options).

and which should I use? ‘maintenanceint’ has to be picked up from DB and can be anything i.e. 1; 3; 5; 9; 12 etc.

You use the value from the database to create a DateInterval object:

$timeToMaintainance = DateTimeInterval::createFromDateString($row_rsHardwareAsset['maintenanceint'].' months');

And then you can use that for any further calculations.

Because of the “row” I’m assuming that is a database result that is assigned to $v. And then $v is used in the line

$date = strtotime(date("Y-m-d", strtotime($date)) . " +$v month");

The line having nested functions makes it bit more difficult to read, But

  • the inner $date string is converted to a date object
  • that date object is formatted into a Y-m-d string
  • that Y-m-d string is concatenated to $v (whatever $v might happen to be) and “month”
  • the concatenated string is converted to a date object

So I have used droopsnoot’s sample, changed it a bit a looks like it does the job.

Now the question, do I need two “$diff = $someDateDatetime->diff($now);” lines within the code or not? I could try to remove one and see if it works but want to know if there is requirement to have it within certain location.

The code looks like this now:

<?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>';
}
?>

No. You need to do it just once. Once the $diff object is populated you can use it’s properties as many times as you want later in the code.

Ok thank you. Makes it clear.

Now I have a need for email reminder which checks every day for the due maintenance items. Shall I create new thread as it’s not really relates to this topic?

Yes, that would be best. Create a new thread with the code as far as you can get it.

Not mine, that was code from @TeNDoLLA, I just merged it with your own code to use your variables.

2 posts were split to a new topic: Regular email reminder

A post was merged into an existing topic: Regular email reminder

Do you have to use DateTime to make diff function work? I now have “nextmaint” date in DB and so no need to calculate it within PHP code.

I only need to know days and months difference.

EDIT: Answered my question by trying different examples I’ve found.