Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MySQL/php date question ~~~ 500pts

Posted on 2004-05-01
7
Medium Priority
?
383 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

610 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