0

I have the following variables:

$days_a = $array_total['days']; // This one display days 
$hours_a = $array_total['hours']; // This one display hours
$minutes_a = $array_total['minutes']; // This one display minutes
$seconds_a = $array_total['seconds']; // This one display seconds

These variables give me the current session time, here all works.

Now the problem...

I have then these variables:

$days_f = $fetch_s['days'];
$hours_f = $fetch_s['hours'];
$minutes_f = $fetch_s['minutes'];
$seconds_f = $fetch_s['seconds'];

These display the time that the user entered in the website previously, they took the values from the database... I did this for add the current session variable values to the values in the MySQL Db:

   $days_2 = $days_a + $days_f;
   $hours_2 = $hours_a + $hours_f;
   $minutes_2 = $minutes_a + $minutes_f;
   $seconds_2 = $seconds_a + $seconds_f;

But I need that each 60 seconds it add 1 to $minutes_2 , each 60 minutes it add 1 to $hours_2 and each 24 hours it add 1 to $days_2

For example if I have 118 seconds I need that it adds 1 to $minutes_2 and set the variable $seconds_2 to 58 etc...

How can this be done?

I tried something like that:

   $seconds = $seconds_2 % 60;
    $minutes_3  = $seconds_2 / 60;
     $minutes  = $minutes_3  % 60;
      $hours_3   = $minutes  / 60;
       $hours    = $hours_3  % 24;
        $days   = $hours  / 24;

          $days = (int)$days_int;

But this won't work... Someone know how do that in a similar or different way?

Could someone give me the code? Please

1
  • 2
    See the examples for DateTime::diff() here. Commented Dec 12, 2014 at 20:54

1 Answer 1

2

In my experience it is easier to do these calculations by converting to UNIX seconds for a given time, adding the seconds, the converting back. That way the system does all the day, hour, minute and seconds book-keeping for you. Use e.g. mktime to convert to UNIX seconds (you can use your existing time parameters), and date, strftime to convert back to day, hour, minutes, seconds.

Sign up to request clarification or add additional context in comments.

4 Comments

I second this answer. Personally, I never even store the data/time written out as a string. I always use UNIX seconds/a UNIX timestamp. Store the timestamp in your database and when necessary, you can easily convert it from a timestamp to a human-readable date/time. By doing this, time modifying arithmetic is made simpler. This also allows you to easily manage timezones by simply converting the UNIX timestamp to whichever timezone you (or a user) would prefer.
This is how i used to do it in php < 5.3. Now i always use full human readable timestamps (2014-01-01 00:00:00) and use DateTime to format and perform calculations. Its a lot heavier than unix time stamps but its easier to work with and is more expressive IMO. What the OP is doing however, is just insane.
Haha, well, the OP is learning. We were all in his shoes at one point. Anyway, yes, DateTime presents better readability, but you gain some advantages with searching the database. You can simply pull up the records of all entries with a timestamp greater than another timestamp. I am unsure if MySQL provides a way to compare string representations of dates and thus perform that same task. I would also imagine that it's more expensive to use DateTime vs. simple arithmetic; however, I could be wrong about that.
I do not understand how do that, could someone give me a code example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.