TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Date and time.. Yikes (http://www.talkphp.com/absolute-beginners/1982-date-time-yikes.html)

Aaron 01-17-2008 01:15 AM

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.

Salathe 01-17-2008 02:12 PM

Quote:

Originally Posted by Aaron (Post 8690)
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 (Post 8690)
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 (Post 8690)
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!). :-)

Aaron 01-18-2008 09:56 PM

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?

xenon 01-18-2008 10:53 PM

umm...the number of seconds between 2 dates / the number of seconds in an hour (3600) should give you the difference in hours.

Aaron 01-19-2008 05:15 AM

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?

xenon 01-19-2008 11:14 AM

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).

Aaron 01-19-2008 05:07 PM

Thank you, that really cleared it up for me.

What is \ why did you use the Ciel() function?

xenon 01-19-2008 05:51 PM

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.


All times are GMT. The time now is 10:24 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0