Jump to content

Need help slightly altering this pretty simple script...


asmith6

Recommended Posts

I found this online at http://php.net/manual/en/function.time.php:

 

<?php

function rel_time($from, $to = null)

{

  $to = (($to === null) ? (time()) : ($to));

  $to = ((is_int($to)) ? ($to) : (strtotime($to)));

  $from = ((is_int($from)) ? ($from) : (strtotime($from)));

 

  $units = array

  (

  "year"  => 31536000, // seconds in a year  (12 months)

  "month"  => 2628000,  // seconds in a month  (4 weeks)

  "week"  => 604800,  // seconds in a week  (7 days)

  "day"    => 86400,    // seconds in a day    (24 hours)

  "hour"  => 3600,    // seconds in an hour  (60 minutes)

  "minute" => 60,      // seconds in a minute (60 seconds)

  "second" => 1        // 1 second

  );

 

  $diff = abs($from - $to);

  $suffix = (($from > $to) ? ("from now") : ("ago"));

 

  foreach($units as $unit => $mult)

  if($diff >= $mult)

  {

    $and = (($mult != 1) ? ("") : ("and "));

    $output .= ", ".$and.intval($diff / $mult)." ".$unit.((intval($diff / $mult) == 1) ? ("") : ("s"));

    $diff -= intval($diff / $mult) * $mult;

  }

  $output .= " ".$suffix;

  $output = substr($output, strlen(", "));

 

  return $output;

}

?>

=======================

 

Basically, if you type <?php echo rel_time("March 12, 2011 7:00 PM"); ?>, it will show you the time ago relative to the current time.

 

But there's two problems. 

1) The time it sends out is about 5hrs early (the actual time is 7:00pm, I set the rel time to 7:00pm, but the time it displays reads '5 hours ago, 1 minute, and 3 seconds ago'. I'm not much of a coder, but judging by the looks of it, it would appear correct... but apparently not.

 

2) My second gripe is that the code shows too many increments of date. What I mean is, if if I set the date to over a year ago, it would display the years,months,weeks,days,minutes,seconds. That is way too much increments. I just want it to display seconds, if I submitted it seconds ago, just display minutes if it was displayed minutes ago, and display days if it was submitted days ago. You get the idea.

 

Friends, family, fellow coders, and bored people- can you help a nooblet?

 

Thanks!

 

 

Link to comment
Share on other sites

Time is based on the server's local time.  So if it's showing 5hrs "earlier" than your time that means your server is in another time zone than you.  You will have to adjust the script accordingly if you want it to display in your time.  You can do this by doing the math manually or else use date_default_timezone_set()

 

 

 

 

Link to comment
Share on other sites

Time is based on the server's local time.  So if it's showing 5hrs "earlier" than your time that means your server is in another time zone than you.  You will have to adjust the script accordingly if you want it to display in your time.  You can do this by doing the math manually or else use date_default_timezone_set()

 

Hold on, before I go on modifying th script, is there any way for it to show the time ago relative to the person's timezone? Meaning, if I submitted something 3hrs ago, it'll show 3hrs ago on my screen, as well as everybody around the world's screen?

Link to comment
Share on other sites

You can't do this with php, as it is server-side.  You have to do it client-side with javascript.

 

Example:

var d = new Date()
var gmtHoursOffset = -d.getTimezoneOffset()/60;

 

This will give you the +/- hours from GMT, relative to user.  So for instance if you are in CST timezone, gmtHoursOffset would be "-6".  Easy enough to get it if you are doing something like processing a form...just include the value as a hidden field. 

 

 

 

Link to comment
Share on other sites

You can't do this with php, as it is server-side.  You have to do it client-side with javascript.

 

Example:

var d = new Date()
var gmtHoursOffset = -d.getTimezoneOffset()/60;

 

This will give you the +/- hours from GMT, relative to user.  So for instance if you are in CST timezone, gmtHoursOffset would be "-6".  Easy enough to get it if you are doing something like processing a form...just include the value as a hidden field.

My familiarity with php is moderately-little (I can understand medium difficulty code, but can only code the simplest php stuff), but my familiarity with javascript is pretty much none.

 

So what you suggest is insert this at the bottom of my html page:

 

<script type="text/javascript">

var d = new Date()

var gmtHoursOffset = -d.getTimezoneOffset()/60;

</script>

 

That's it right? And for php, I would still use the date_default_timezone_set, as you suggested earlier, correct?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.