Solved

MySQL/php date question ~~~ 500pts

Posted on 2004-05-01
7
380 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
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.
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.

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