time recounting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cereal
Forum Newbie
Posts: 8
Joined: Wed Oct 02, 2002 10:27 am
Location: Belgium
Contact:

time recounting

Post 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?
User avatar
Elmseeker
Forum Contributor
Posts: 132
Joined: Sun Dec 22, 2002 5:48 am
Location: Worcester, MA

Post 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.
User avatar
cereal
Forum Newbie
Posts: 8
Joined: Wed Oct 02, 2002 10:27 am
Location: Belgium
Contact:

Post by cereal »

thanks a lot :!:

:D
User avatar
Elmseeker
Forum Contributor
Posts: 132
Joined: Sun Dec 22, 2002 5:48 am
Location: Worcester, MA

Post 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.
Last edited by Elmseeker on Wed Jan 08, 2003 12:26 pm, edited 1 time in total.
User avatar
cereal
Forum Newbie
Posts: 8
Joined: Wed Oct 02, 2002 10:27 am
Location: Belgium
Contact:

Post by cereal »

yep it does, just tested it.
User avatar
Elmseeker
Forum Contributor
Posts: 132
Joined: Sun Dec 22, 2002 5:48 am
Location: Worcester, MA

Post 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. :)
User avatar
cereal
Forum Newbie
Posts: 8
Joined: Wed Oct 02, 2002 10:27 am
Location: Belgium
Contact:

Post 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
laserlight
Forum Commoner
Posts: 28
Joined: Wed Jan 01, 2003 6:41 am

Post 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.
Post Reply