Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

comapring dates php

I have a date stored in the foormat dd/mm/yyyy. I want to retireve this date from the database and compare it with todays date. I want to use the mktime function to find out is the stored date is in the past. This is what I have so far. I feel I am getting close. Please help!



<?
 
session_start();
include ('../shared/connect.php');

$query= "SELECT headline, id, expiry, link FROM events";

 $result= mysql_query($query);

 
   while  ($row = @mysql_fetch_array($result, MYSQL_ASSOC)){


$compare= explode("/", $row['expiry']);


$day = isset($compare[0]) ? $compare[0] : null;
echo "day is $day<br>br><br>";
$month = isset($compare[1]) ? $compare[1] : null;
echo "month is $month<br>br><br>";
$year = isset($compare[2]) ? $compare[2] : null;
echo "year is $year<br>br><br>";

$then = mktime(0,0,0,$month,$day, $year);
$today= date("d m y");
$explodetoday = explode("/", $today);
$now = mktime(0, 0, 0, $explodetoday[1], $explodetoday[0], $explodetoday[2]);

$diff = $now - $then;

echo "today is $today<BR>";
echo "expiry date is $ross<br>";
echo "now mktime value is is $now<br>";
echo "then mkvalue is $then<br><br>";
if ($diff < 0 ) {
/*$text = stripslashes ($row['headline']);
            $newtext = wordwrap($text, 12, "\n", 1);
            $link=$row['link'];
            echo $newtext."<a href=\"$link\">...read more</a>"."<br /><br />";*/
 
 }
 }
?>
0
roscoeh23
Asked:
roscoeh23
  • 3
  • 2
1 Solution
 
WelkinMazeCommented:
Hi,

What is not working? It seems fine to me at first glance.
0
 
WelkinMazeCommented:
Maybe a missing "<" in these ones:
echo "day is $day<br>br><br>";
echo "month is $month<br>br><br>";
echo "year is $year<br>br><br>";

And replace null with 0 here:
$day = isset($compare[0]) ? $compare[0] : null;
$month = isset($compare[1]) ? $compare[1] : null;
$year = isset($compare[2]) ? $compare[2] : null;
0
 
roscoeh23Author Commented:
you can see it here. I have missed out a < or two but the priblem is the them value is always the same!

http://nationalservicesscotland.com/cms/time.php
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
roscoeh23Author Commented:
You can see it here. I have missed out a < or two but the problem is the $then value is always the same!
0
 
WelkinMazeCommented:
Hm, it doesn't seem the same to me at the link you've posted. The second and the third one are the same but the first one is different. Maybe the second and the third one are the same in the database.
0
 
lostcarparkCommented:
The problem would seem to be here:

$today= date("d m y");
$explodetoday = explode("/", $today);
$now = mktime(0, 0, 0, $explodetoday[1], $explodetoday[0], $explodetoday[2]);

You're formatting the date seperated by spaces, then exploding it on the slash character. There won't be any slashes to split on so the whole date will be in the first element of the array. An easy fix would be just to change the "/" to a space in the explode function, but why go to the trouble of formatting the whole date as a string only to break it up again. You could simply replace the three lines with:

$now = mktime (0,0,0,date("m"),date("d"),date("Y"));

There also seems to be an issue with this line:

echo "expiry date is $ross<br>";

Since the variable $ross is not assigned anywhere. Probably a leftover of your testing. I think it should be:

echo "expiry date is $row[expiry]<br>";

Finally, its useful to note that:

$diff = ($now - $then) / 86400;

Will give you the number of days between the two dates (86400 is the number of seconds in a day).

Hope this is helpful,

James
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now