hi,

i want convert the time stamp unix format to hours and days

i have this:

$date = trim(date('H:i:s', $ofwhat));

 $ida =  trim(date('d', $ofwhat)) -1;

           if ($ida >0) {if ($ida==1) { $dia= $ida. " day, "; } }

           if ($ida >0) { if ($ida>1) { $dia= $ida. " days, "; } }

this work fine in my local pc that have php 4
if i put this:

echo unix2data(3600,4);

they return 1 hour that is correct

but when i put in the server running php 5 the

echo unix2data(3600,4);

return 19 hours!
what i doing wrong?
thanks a lot for your help

Dani AI

Generated

Short answer for : the problem is using PHP date functions to format a duration. date() (and similar) treat the integer as a Unix timestamp (a point in time) and then apply the server timezone. That makes a 3600-second value show different hour values on machines with different timezones. was right to ask what unix2data does, and ’s timezone pointer is on target — but the correct fix is to treat the value as a duration, not as a timestamp.

Two safe approaches:

  • If the interval is always less than a day, gmdate() will format hours/minutes/seconds without applying timezone offsets (it uses GMT). See the manual for gmdate.
  • For general durations (days + hours + minutes + seconds) do integer math on the total seconds and build the string yourself. That avoids timezone pitfalls entirely.

Example function (robust and timezone-independent):

function format_seconds($secs) {
  $days = (int) floor($secs / 86400);
  $secs %= 86400;
  $hours = (int) floor($secs / 3600);
  $secs %= 3600;
  $minutes = (int) floor($secs / 60);
  $seconds = $secs % 60;

  $parts = array();
  if ($days) $parts[] = $days . ' day' . ($days>1 ? 's' : '');
  if ($hours) $parts[] = $hours . ' hour' . ($hours>1 ? 's' : '');
  if ($minutes) $parts[] = $minutes . ' minute' . ($minutes>1 ? 's' : '');
  $parts[] = $seconds . ' second' . ($seconds>1 ? 's' : '');

  return implode(', ', $parts);
}

Quick troubleshooting tips: check the server PHP timezone (phpinfo() or call date_default_timezone_get()), or set a script-local timezone with date_default_timezone_set('UTC') to force consistent behavior (see date_default_timezone_set). If you post the unix2data code, it will be easier to point out the exact line to change.

Recommended Answers

All 4 Replies

is the value of unixtime 3600 = 1 hour

edit your timezone in php.ini

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = "Asia/Manila" //edit your timezone here

click here for the rest of timezone http://php.net/manual/en/timezones.php, after your edit save changes and restart your PHP server

the OP is 'interesting'
but a fine example of what not to do

write what you wish to achieve,
you have a function unix2data that you do not define for anyone to see,
nor do you explain the expected results, or the expected inputs

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.