• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • 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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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