php Date()

driveflexfuel

New Member
Messages
159
Reaction score
0
Points
0
I am trying to compare the difference between two times.

$time1 = 2009-04-27 09:18:10;
$time2 = 2009-04-27 11:18:10;

I am using this code to restrict use to one time per hour. If anyone can help out i would highly appreciated.
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
unix style time stamps (using time() ) would be easier. It returns the number of seconds since the unix epoch (1/1/1970 I believe). There's 3600 seconds in an hour, so you can take time1 - time2 and if it's greater than 3600 more than 1 hour has passed.
 

freecrm

New Member
Messages
629
Reaction score
0
Points
0
Here's one answer.. expanding on garrettroyce's answer.

Start Sessions at the top of the pages.

PHP:
session_start()

On first time use.

PHP:
$_SESSION['firstusetime']=Time();
$_SESSION['nexttrytime']= $_SESSION['firstusetime'] + 3600;

On next attempt

PHP:
if ((Time() - $_SESSION['firstusetime']) <3600) {

echo "<br/>Sorry, you have already used this funtion within the last hour.  Please try again after ".date("Y-m-d H:i:s", $_SESSION['nexttrytime']);

} else {

//execute code

}

There are a number of ways to get round this, but this is pretty simple.
 
Last edited:

driveflexfuel

New Member
Messages
159
Reaction score
0
Points
0
I think i need to supply some ore information on what I am doing.

time1 comes from a database entry such as $con['time'] the format is:
$time1 = 2009-04-27 09:18:10;

I do not want to use session variables. I am looking to do it in normal php variables.
Edit:
This is what my solution was:

$characters = array(" ", "-", ":");

$time1 = $con['time'];
$time1 = str_replace($characters, ",", "$time1");
list($year, $month, $day, $hour, $minute, $second) = split(',', $time1);
$time1 = mktime($hour,$minute,$second,$month,$day,$year);

I did this with both times and then subtracted the two to get the number of seconds.
 
Last edited:

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
Yeah, your method sounds pretty good. I almost always use unix timestamps for everything, so that they can more easily be converted and modified with regular arithmetic
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Alternative timestamp conversions:
strtotime will parse a string into a Unix timestamp; it's less efficient than other approaches (such as yours), but not overly so and is very succinct. strptime and date_parse_from_format will efficiently parse a date/time string to an array. You can even use sscanf for a one-liner:
PHP:
$time1=call_user_func_array('mktime', sscanf('2009-04-27 09:18:10', '%6$d-%4$d-%5$d %1$d:%2$d:%3$d'));

Eventually you'll be able to use DateTime and DateInterval for date/time arithmetic.
 

kapisco

New Member
Messages
5
Reaction score
0
Points
0
PHP:
$time1 = '2009-04-27 09:18:10';
$time2 = '2009-04-27 11:18:10';


$time_diff = strtotime($time2) - strtotime($time1);//in seconds
echo ($time_diff / 3600);//in hours

here's the solution dude enjoy ;)
 
Top