time recounting
Moderator: General Moderators
time recounting
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?
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?
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.
Usage Examples:
The output should look like this:
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;
}
?>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>";
?>a and b should be rather self explanatory...(a)head and (b)ehind...enjoy it.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.
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. 
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.
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.
Last edited by Elmseeker on Wed Jan 08, 2003 12:26 pm, edited 1 time in total.
Glad to hear it, got the decimal place thing working too, here is the new code for that.
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. 
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;
}
?>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
edge.dev.box.sk
and for these sites.
japan.box.Sk
and maybe dunno if they willl use it.
neworder.box.sk
-
laserlight
- Forum Commoner
- Posts: 28
- Joined: Wed Jan 01, 2003 6:41 am
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:
Rather than use a direction parameter, this relies on the offset (in hours) being negative to go back the number of hours.
This function would also work in that case:
Code: Select all
function mydate($offset) {
$offset *= 3600;
return gmdate("h:ia, d M Y", mktime() + $offset);
}