FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » MySQL date time current time diffenece
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
MySQL date time current time diffenece [message #175188] Sun, 21 August 2011 13:42 Go to next message
j is currently offline  j
Messages: 9
Registered: July 2011
Karma: 0
Junior Member
I have a timestamp in MySQL table. I want to compare that to the
current time. I'll be using this code on different servers.

I can do this:

$mysql_datetime = '2011-08-19 14:00:35';

$mysql_datetime_formatted = preg_replace('/\D/','',$mysql_datetime);

$date_now = date("YmdHis");

$diff = $date_now - $mysql_datetime_formatted;

echo "$mysql_datetime_formatted - $date_now = $diff in seconds";

Can I count on (more or less) the MySQL time/time zone to be the same as
that PHP uses?

More reliable or easier way?

Jeff
Re: MySQL date time current time diffenece [message #175189 is a reply to message #175188] Sun, 21 August 2011 14:15 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 8/21/2011 9:42 AM, j wrote:
> I have a timestamp in MySQL table. I want to compare that to the current
> time. I'll be using this code on different servers.
>
> I can do this:
>
> $mysql_datetime = '2011-08-19 14:00:35';
>
> $mysql_datetime_formatted = preg_replace('/\D/','',$mysql_datetime);
>
> $date_now = date("YmdHis");
>
> $diff = $date_now - $mysql_datetime_formatted;
>
> echo "$mysql_datetime_formatted - $date_now = $diff in seconds";
>
> Can I count on (more or less) the MySQL time/time zone to be the same as
> that PHP uses?
>
> More reliable or easier way?
>
> Jeff

Try asking in comp.databases.mysql - there are better ways to do it.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: MySQL date time current time diffenece [message #175204 is a reply to message #175188] Mon, 22 August 2011 10:03 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Sun, 21 Aug 2011 09:42:41 -0400, j wrote:

> Can I count on (more or less) the MySQL time/time zone to be the same as
> that PHP uses?

I'd expect that on a competently administered mysql server, the date and
time functions would always be in a consistent timezone, but given that
the mysql server could be in a different continent to the system that php
is running on, I wouldn't assume that they will always be in the same
timezone.

If you need to do a lot of tz sensitive work, it *might* be more robust
to use php date and time functions, setting the appropriate php timezone,
and store and read the values as UTC integer seconds based on your
preferred epoch.

Rgds

Denis McMahon
Re: MySQL date time current time diffenece [message #175205 is a reply to message #175188] Mon, 22 August 2011 12:06 Go to previous message
Jonathan Stein is currently offline  Jonathan Stein
Messages: 43
Registered: September 2010
Karma: 0
Member
Den 21-08-2011 15:42, j wrote:

> echo "$mysql_datetime_formatted - $date_now = $diff in seconds";

If my fast interpretation of your code is right, this is only true for
intervals less than a minute. If the difference is exactly one minute,
your result will say 100 seconds.

> Can I count on (more or less) the MySQL time/time zone to be the same as
> that PHP uses?

Is "more or less" good enough for you? Often yes, but sometimes not...

> More reliable or easier way?

Look at "TIMESTAMPDIFF()", "NOW()" and the other date and time functions
in MySQL and let the database do all the work for you. - Then you're
also sure all calculations are based on the same timezone.

Regards

Jonathan
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Webhosting, Domain Name, Data Center Cyprus, Hosting Services
Next Topic: Roll SQL name value query into two-dimensional array
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Nov 26 18:27:23 GMT 2024

Total time taken to generate the page: 0.02241 seconds