0

I can convert an int to days, hours, minutes and seconds, but what is the calculation to reverse that and convert back to seconds?

Here's what I have for the first conversion:

<?php
$seconds = 12345;
$days = floor ($seconds / 60 / 60 / 24);
$hours = floor ($seconds / 60 / 60) % 24;
$minutes = floor ($seconds / 60) % 60;
$seconds = $seconds % 60;
?>
Asa Carter
  • 2,207
  • 5
  • 32
  • 62
  • 1
    days usually have 24 hours. and so 24*60 minuten. and os 24*60*60 seconds. (except days where daylightsavingtime starts). So where one way you use / ,now use * – Ivo P Jul 12 '17 at 11:11
  • 1
    I believe you can use `strtotime` and it will change it to a timestamp (int) – treyBake Jul 12 '17 at 11:12
  • @asa-carter Are you kidding? – axiac Jul 12 '17 at 11:12
  • https://stackoverflow.com/a/20596966/4934273 – Md. Abutaleb Jul 12 '17 at 11:12
  • but question remains, why? What is the goal of this. Wouldn't there be another route to solve your problem, as this seconds issue seems not to be the optimal way – Ivo P Jul 12 '17 at 11:12
  • @IvoP Time is stored as an int the database, but I need to split it into D, H, M, S for an API, and then save back to the DB. – Asa Carter Jul 12 '17 at 11:14

2 Answers2

1
$total = ($days * 24 * 60 * 60) + ($hours * 60 * 60) + ($minutes * 60) + $seconds;
Krunal
  • 77,632
  • 48
  • 245
  • 261
localheinz
  • 9,179
  • 2
  • 33
  • 44
0

try this

$total = ($days * 24 * 60 * 60) + ($hours * 60 * 60) + ($minutes * 60) + $seconds;
Mahipal Patel
  • 543
  • 3
  • 15