Page 1 of 1

time recounting

Posted: Wed Jan 08, 2003 11:42 am
by cereal
what i'm trying to do,
well i got an unix timestamp, and i would like to display it but some hours back or ahead.

timestamp: 10800
convertion of time stamp: Thursday, January 1st 1970, 4:00:00
hours back: 10
new time: Monday, December 31 1969, 18:00:00

Does anyone know such an function?

Posted: Wed Jan 08, 2003 12:14 pm
by Elmseeker
Just threw this together for ya, probably not the best or most efficient way of doing this but it's what I thought off off the top of my head.

Code: Select all

<?function timewarp($direction, $diff) {
if ( $direction == "a" ) {
$td = time() + ($diff*3600);
$nt = date( "l M jS Y H:i:s", $td);
} else if ( $direction == "b" ) {  
$td = time() - ($diff*3600);
$nt = date( "l M jS Y H:i:s", $td);
} else {
echo "Error, time direction invalid. Valid directions are a and b.";
}
echo $nt;
}
?>
Usage Examples:

Code: Select all

<?
$cd = time();
$curdate = date( "l M jS Y H:i:s", $cd);
echo $curdate."<BR>";
timewarp("a", 5);
echo "<BR>";
timewarp("b", 5);
echo "<BR>";
timewarp("c", 5);
echo "<BR>";
?>
The output should look like this:
Wednesday Jan 8th 2003 10:15:11
Wednesday Jan 8th 2003 15:15:11
Wednesday Jan 8th 2003 05:15:11
Error, time direction invalid. Valid directions are a and b.
a and b should be rather self explanatory...(a)head and (b)ehind...enjoy it.

Posted: Wed Jan 08, 2003 12:20 pm
by cereal
thanks a lot :!:

:D

Posted: Wed Jan 08, 2003 12:24 pm
by Elmseeker
No problem, forgot to mention that $diff is the number of hours you want to show. Also, this could easily be modified to change the time by seconds, minutes, days, months or even years. Hope this works well for what you need it for. :P

It is not currently working with decimal values but I will look into that, would be nice to ba able to do something like timewarp("a", 1.5);
Dunno, I'll let ya know how that turns out.

Posted: Wed Jan 08, 2003 12:25 pm
by cereal
yep it does, just tested it.

Posted: Wed Jan 08, 2003 12:39 pm
by Elmseeker
Glad to hear it, got the decimal place thing working too, here is the new code for that.

Code: Select all

<?
function timewarp($direction, $diff) {      
settype ( $diff, "float");       
if ( $direction == "a" ) {  
$td = time() + ($diff*3600);
$nt = date( "l M jS Y H:i:s", $td);
} else if ( $direction == "b" ) {
$td = time() - ($diff*3600);
$nt = date( "l M jS Y H:i:s", $td);
} else {
echo "Error, time direction invalid. Valid directions are a and b.";
}
echo $nt;
}
?>
Have fun, Also, please send me the URL so I can take a look at your site, I'd really like to see how you're using this. :)

Posted: Wed Jan 08, 2003 1:56 pm
by cereal
well it will be for an webportal, called the edge engine.

edge.dev.box.sk

and for these sites.

japan.box.Sk
and maybe dunno if they willl use it.
neworder.box.sk

Posted: Thu Jan 09, 2003 5:04 am
by laserlight
btw, do you intend to use this 'x hours back' thing to display, say, the current time?

This function would also work in that case:

Code: Select all

function mydate($offset) &#123;
	$offset *= 3600;
	return gmdate("h:ia, d M Y", mktime() + $offset);
&#125;
Rather than use a direction parameter, this relies on the offset (in hours) being negative to go back the number of hours.