Forum Moderators: coopster

Message Too Old, No Replies

Difference between SQL datetime using PHP without epoch?

         

JAB Creations

8:31 am on Jul 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Example SQL datetime stamp: 2010-07-27 04:05:04.

I am trying to figure out how to subtract the current datetime (date('Y-m-d H:i:s')) from an existing datetime stamp without resorting to the Linux epoch as to avoid giving myself a heart-attack in 2038. I am posting this in the PHP forum as I do not desire to outsource this to SQL. This is intended for an anti-flooding script to keep users of my software from posting certain things more then once in X number of seconds.

Suggestions please?

- John

penders

9:07 am on Jul 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



By 2038 we'll all be on 64-bit systems (at least) and this won't be a problem.

Currently, if you want to manipulate dates outside of the 32-bit range (1901 - 2038) then either use 64 bit PHP on a 64 bit platform or use the DateTime class [uk2.php.net] (which internally stores dates as 64-bit integers). However, some of the more useful methods (and additional classes) like sub() and DateInterval (to subtract one date from another) are only available under PHP 5.3. However, you might be able to muddle something together with the modify() method, or if you just want to subtract one date from another you can do this manually by splitting the DateTime into its component parts and doing some date math.

JAB Creations

9:59 am on Jul 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the reply Penders. I'm using 64 bit XP though I'm not sure how to determine if PHP is 32 bit or 64 bit though for local testing I simply use XAMPP. I'm not sure what the live server is running nor how to check.

Locally I'm running PHP 5.3 however the live server is 5.2.

Any way I messed with the DateTime class though I have not gotten the greater than less than operators to work and will mess with this more after some sleep. :)

- John

<?php

$date0 = new DateTime('2010-07-27 05:47:11');

$date11 = new DateTime();
$date111 = $date11->format('Y-m-d H:i:s');
$date12 = date_modify($date11,'-30 seconds');
$date13 = $date12->format('Y-m-d H:i:s');

echo $date111.'<br /><br />'.$date13;


echo '<br /><br /><br /><br />';

if ($date0>$date111) {echo '1';}
else if ($date0<$date111) {echo '2';}
?>

JAB Creations

7:29 am on Jul 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is what I have working now requiring PHP 5.3 with fallback support for PHP 5.2. I figure most people won't be running PHP 5.2 in five or ten years.

- John

<?php
$date0 = new DateTime('2010-07-28 03:24:10');
$date1 = date_format($date0,'Y-m-d H:i:s');
echo '<div>'.$date1.'</div>';

$date2 = new DateTime();
$date3 = date_format($date2,'Y-m-d H:i:s');
echo '<div>'.$date3.'</div>';

if (function_exists(date_diff))
{
// PHP 5.3
$interval = $date0->diff($date2);
echo '<div>'.$interval->format('%s seconds').'</div>';

if ($interval->format('%d')=='0' && $interval->format('%h')=='0' && $interval->format('%s')<30) {echo 'do not post';}
else {echo 'can post';}
}
else
{
// PHP 5.2
echo '<div>'.strtotime($date1).'</div>';
echo '<div>'.strtotime($date3).'</div>';
$date4 = strtotime($date3)-strtotime($date1);
echo '<div>'.$date4.'</div>';

if ($date4>=30) {echo 'can post';}
else {echo 'do not post';}
}
?>