Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

PHP filter by Date

Guest
Nov 02, 2009 Nov 02, 2009

Hi Chaps,

I have a bit of PHP code, that at the moment doesn't work.
I'm trying to show all the 'overdue' projects in a different style:

PHP Code:

<?php if ($row_rsProjects['projdue'] == '= DATE(NOW())') { ?>

      <tr class="overdue">
      <?php } else if ($row_rsProjects['projdue'] == '< DATE(NOW())') { ?>
      <tr class="duetoday">
      <?php } else { ?>
      <tr class="within">
      <?php }?>

The

Quote:
'= DATE(NOW())'

works with MySQL, but I don't know how to get it to work with PHP. Any ideas?

TOPICS
Server side applications
459
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Nov 02, 2009 Nov 02, 2009

DATE(NOW()) is MySQL code. As you have discovered, it doesn't work in PHP.

It's not just a question of using the wrong code, MySQL and PHP handle dates in totally incompatible ways. PHP uses Unix timestamps, which count the number of seconds since the beginning of January 1970. MySQL stores dates in the ISO recommended YYYY-MM-DD HH:MM:SS format.

There are several ways you could do this, but the following should work:

// get the date parts from the MySQL date

$dateparts = preg_split('/[-\s]/', $row_

...
Translate
LEGEND ,
Nov 02, 2009 Nov 02, 2009
LATEST

DATE(NOW()) is MySQL code. As you have discovered, it doesn't work in PHP.

It's not just a question of using the wrong code, MySQL and PHP handle dates in totally incompatible ways. PHP uses Unix timestamps, which count the number of seconds since the beginning of January 1970. MySQL stores dates in the ISO recommended YYYY-MM-DD HH:MM:SS format.

There are several ways you could do this, but the following should work:

// get the date parts from the MySQL date

$dateparts = preg_split('/[-\s]/', $row_rsProjects['projdue']);

// get the Unix timestamp for midnight at the beginning of the due date

$projdue = mktime(0,0,0, $dateparts[1], $dateparts[2], $dateparts[0]);

// get the Unix timestamp for midnight at the beginning of today

$today = mktime(0,0,0, date('n'), date('j'), date('Y'));

if ($today == $projdue) {

  // the project is due today

} elseif ($today > $projdue) {

  // the project is overdue

} else {

  // the project is not yet due

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines