Solved

MySQL/php date question ~~~ 500pts

Posted on 2004-05-01
7
376 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
Comment Utility
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
Comment Utility
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
Comment Utility
please elaborate!
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 14

Expert Comment

by:cracky
Comment Utility
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
Comment Utility
Ok thanks cracky - that should do the trick

bc :-)
0
 
LVL 14

Accepted Solution

by:
cracky earned 500 total points
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Read about why website design really matters in today's demanding market.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now