TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-17-2008, 01:15 AM   #1 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default 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.
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 01-17-2008, 02:12 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Aaron View Post
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 View Post
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 View Post
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!).
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Aaron (01-18-2008)
Old 01-18-2008, 09:56 PM   #3 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default

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?
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 01-18-2008, 10:53 PM   #4 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

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.
xenon is offline  
Reply With Quote
Old 01-19-2008, 05:15 AM   #5 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default

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?
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 01-19-2008, 11:14 AM   #6 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

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.
xenon is offline  
Reply With Quote
The Following User Says Thank You to xenon For This Useful Post:
Aaron (01-19-2008)
Old 01-19-2008, 05:07 PM   #7 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default

Thank you, that really cleared it up for me.

What is \ why did you use the Ciel() function?
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 01-19-2008, 05:51 PM   #8 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

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.
xenon is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 10:10 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design