 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
01-17-2008, 01:15 AM
|
#1 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
|
Date and time.. Yikes
PHP Code:
<?php $timestamp = time(); $oldtimestamp = mktime(5,50,23,4,30,2005); //5:50.23 on April 30, 3005 $difference = $timestamp - $oldtimestamp; $getdate = getdate($difference); print "The date is "; print date("M-D-Y"); print "<br />"; print "The time is "; print date("g:i"); print "<br /><br />";
print "<br /><br />"; print "This makes a timestamp of $timestamp"; print "<br />"; print "We will also be using the timestamp $oldtimestamp.<br />"; print "The difference is $difference"; print "<br />"; print "If you format that, it comes out as a "; print $getdate[hours]; print " hour difference."; //This does not tell the difference in time, just the difference in the hour of the day. print "<br />";
//Whats left undone... //I need to find a way to calculate the time elapsed between two timeestamps, and execute an action when that time exceeds another time. I also need to find a way to add time to it. For instance, I want to stop displaying something a month from now, but if somone submits a form, it will stop displaying two months from now. ?>
So, anyway, as you can see I have been toying around with time lately... How do I do what is commented at the bottem? One thing I neglected to add was that I need a way to do it in real time.
|
|
|
01-17-2008, 02:12 PM
|
#2 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Quote:
Originally Posted by Aaron
I need to find a way to calculate the time elapsed between two timeestamps
|
A timestamp is literally just an integer of the number of seconds since midnight on January 1st 1970. To calculate the difference you can just do a simple subtraction to get the number of seconds difference between the two timestamps. If you want the result to be in another unit (minutes, days, weeks, etc.) then you can just scale that difference in seconds appropriately.
For example:
PHP Code:
<?php
$iTimeStart = 1200556800; // 17 Jan 2008, 8:00am UTC (GMT)
$iTimeStop = 1200643200; // 18 Jan 2008, 8:00am UTC (GMT)
// abs() is used here to make sure the result
// is always a positive (well, unsigned) integer
$iTimeDiff = abs($iTimeStart - $iTimeStop);
// The difference between 17 Jan 2008, 8:00am UTC and 18 Jan 2008, 8:00am UTC is 86400 seconds.
printf(
'The difference between <em>%s</em> and <em>%s</em> is <em>%s seconds</em>.',
date('d M Y, g:ia e', $iTimeStart),
date('d M Y, g:ia e', $iTimeStop),
$iTimeDiff
);
Quote:
Originally Posted by Aaron
and execute an action when that time exceeds another time.
|
All that we need to do here is to check if the difference between the two timestamps is greater than whatever the time 'limit' is (in seconds).
PHP Code:
$iTimeLimit = 12 * 60 * 60; // 12 hours = 12 * 60 * 60 seconds
$iTimeNow = time();
$iTimeThen = strtotime('-1 day');
// *should* say "Yes, 1 day is..."
if ($iTimeNow - $iTimeThen > $iTimeLimit)
{
echo 'Yes, 1 day is longer than 12 hours!';
}
else
{
echo 'Well done, you can bend the timeline!';
}
Quote:
Originally Posted by Aaron
I also need to find a way to add time to it.
|
Adding time is just as easy, in fact maybe even easier depending on the route that you wish to take. You can simple add seconds onto the timestamp integer, or you can make use of the strtotime function (much like you can with taking time away, as in the previous example).
PHP Code:
$iTimeNow = time();
$iTimeAdd = $iTimeNow + 86400; // Add 8,400 seconds (one whole day) to now
echo 'New time is ', date('d M Y, g:i:sa e', $iTimeAdd);
$iTimeAdd = strtotime('+1 day', $iTimeNow); // Again, add one whole day to now
echo 'New time is ', date('d M Y, g:i:sa e', $iTimeAdd);
Hope that provided a starting point (or at least, didn't miss the mark completely!). 
|
|
|
|
|
The Following User Says Thank You to Salathe For This Useful Post:
|
|
01-18-2008, 09:56 PM
|
#3 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
|
I get it for the most part... But, the time, if you use
getdate($difference)
$getdate[hours];
like I did above, it gives the hour difference in the day. Not the total number of hours between the two times. And how would I make this happen in real time?
|
|
|
01-18-2008, 10:53 PM
|
#4 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
|
umm...the number of seconds between 2 dates / the number of seconds in an hour (3600) should give you the difference in hours.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
Last edited by xenon : 01-19-2008 at 11:10 AM.
|
|
|
|
01-19-2008, 05:15 AM
|
#5 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
|
OHHH!!!
I totally didn't read Salathe's first paragraph.
Is there a reference to all the seconds in a day/month and such, or do I need to do a lot of math?
|
|
|
01-19-2008, 11:14 AM
|
#6 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
|
You'll have to perform the calculations yourself...but why do you think it's such a pain? It's simple math. Check this out:
PHP Code:
$today = time(); $tommorrow = $today + ( 60 * 60 * 24 );
$sec_diff = $tommorrow - $today; $hour_diff = ceil( $sec_diff / ( 60 * 60 ) ); // because an hour has 60 minutes, with 60 seconds each
echo $hour_diff; // this is 24 (hours)
This example is very simple, because I just wanted to illustrate the technique itself, so it can be easy to understand).
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
|
|
|
|
|
The Following User Says Thank You to xenon For This Useful Post:
|
|
01-19-2008, 05:07 PM
|
#7 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
|
Thank you, that really cleared it up for me.
What is \ why did you use the Ciel() function?
|
|
|
01-19-2008, 05:51 PM
|
#8 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
|
The ceil function is used to round up a floating point number. (ex: ceil(1.79) = 2). Of course, you may want to use floor (to round down a number - floor(1.79) = 1), which is more appropriate than ceil in cases like this one, because 67 minutes means 1 hour and 7 minutes, not 2 hours. But you will have to decide for yourself what you need.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|