Solved

MySQL/php date question ~~~ 500pts

Posted on 2004-05-01
7
378 Views
Last Modified: 2012-06-27
Heeey! So wonderful to see you here!

I'm using MySQL & PHP

I want to get the dates from the database, and compare them to the current date using php. (not an SQL query) I'd like to leave the database how it is; i.e. dates formatted as YYYY-MM-DD, and do the conversions in PHP.

So, basically I want to do this: 'if variableDate is more recent than currentDate, do somehthing'

Also, I will want to insert dates back into the database, formatted correctly.

Thanks in advance for your help!

bc :~)
0
Comment
Question by:bitter_chicken
  • 4
  • 2
7 Comments
 
LVL 1

Expert Comment

by:Darix
ID: 10967347
you need to get dates from database using UNIX_TIMESTAMP(date_field) and in PHP you can compare dates with ex. time function will return current timestamp.
0
 
LVL 14

Expert Comment

by:cracky
ID: 10967456
To get into more detail:

Your query would look something like this:

<?php
$q = "SELECT UNIX_TIMESTAMP(`datefield`) AS `uts` FROM `table`";
$rs = mysql_query($q);
?>

You would then be able to compare the date in PHP like so:

<?php
while ($r = mysql_fetch_object($rs)) {

  if ($r->uts > time()) {
    $mysqltime = strftime("%Y-%m-%d %H:%M:%S",$yourtimestamp);
  } else {
    // Do nothing
  }

}
?>

Note that you can use PHP to convert a timestamp into a MySQL formatted time using strftime("%Y-%m-%d %H:%M:%S",$yourtime).

Please note that if variableDate is more recent than currentDate NOW(), then you are talking about a date in the future.

Does this all make sense?
0
 
LVL 5

Author Comment

by:bitter_chicken
ID: 10967457
please elaborate!
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 14

Expert Comment

by:cracky
ID: 10967476
Another thing bitter_chicken. If your questions are exclusively about PHP / MySQL, you would probably find the PHP & Databases section will get you a more rapid response from more specialised experts:

http://www.experts-exchange.com/Web/Web_Languages/PHP/PHP_Databases/
0
 
LVL 5

Author Comment

by:bitter_chicken
ID: 10967509
Ok thanks cracky - that should do the trick

bc :-)
0
 
LVL 14

Accepted Solution

by:
cracky earned 500 total points
ID: 10967547
Maybe it's easier just to do it all in PHP:

If you extract a date from MySQL as YYYY-MM-DD, you can get the unix timestamp for that date by using the PHP function strtotime("$mysqltime")

You can then compare it to the current timestamp by using the PHP function time() to give you the current timestamp.

E.G:

$mysqltime = "2004-02-01";
$uts_mysqltime = strtotime($mysqltime);

Thus, $mysqltime < time() will evaluate to true, since 2004-02-01 is less than the current date.

You can then use strftime("%Y-%m-%d %H:%M:%S",$timestamp) to re-format any timestamp back to MySQL formatted time. If you only need the date portion and not the time, use strftime("%Y-%m-%d",$yourtime).

For more information on all of these functions, see:

http://www.php.net/manual/en/function.time.php
http://www.php.net/manual/en/function.strtotime.php
http://www.php.net/manual/en/function.strftime.php
0
 
LVL 14

Expert Comment

by:cracky
ID: 10967571
FYI, if you didn't know, the Unix Timestamp is the number of seconds since January 1 1970 00:00:00 GMT. Many *nix based programs use it and in many cases it's easier to use timestamps to compare dates, then just use strftime() to convert it back to MySQL format.
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
This video teaches users how to migrate an existing Wordpress website to a new domain.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question