PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old August 11th, 2002, 11:54 AM
eokorie eokorie is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 7 eokorie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Date Comparison problem

I wrote this script so that I could use if for one of my pages. Basically when you look at it, it is quite simple. Based on the DateAdd function of ASP, a user sets a particular date for an event to be displayed. If the current date is equal to the set date, the event will be displayed. if not, it will not be.

Everything seems to check out fine, the code and everything. but when i run it, i get this output:

Selected Expiry Date: 10/07/2000
Current Date: 11/08/2002

Item Expiry Date: 11/07/2000


1

Clearly, here the current date is higher than the set date (item expiry date), so it should display "2", but it still displays "1". maybe i missed something somewhere, but I would be glad if anyone could have a look at the code blow and show me where my mistake is.

Thanks,

Emmanuel

<?php
function DateAdd ($interval, $number, $date) {

$date_time_array = getdate($date);

$hours = $date_time_array["hours"];
$minutes = $date_time_array["minutes"];
$seconds = $date_time_array["seconds"];
$month = $date_time_array["mon"];
$day = $date_time_array["mday"];
$year = $date_time_array["year"];

switch ($interval) {

case "yyyy":
$year +=$number;
break;
case "q":
$year +=($number*3);
break;
case "m":
$month +=$number;
break;
case "y":
case "d":
case "w":
$day+=$number;
break;
case "ww":
$day+=($number*7);
break;
case "h":
$hours+=$number;
break;
case "n":
$minutes+=$number;
break;
case "s":
$seconds+=$number;
break;

}
$timestamp = mktime($hours ,$minutes, $seconds,$month ,$day, $year);
return $timestamp;
}
?>

<?php
$curDate = date("d/m/Y");
$SelectedDate = $mine = strtotime("10 July 2000");
echo "Selected Expiry Date: " . date("d/m/Y", $SelectedDate), "n";
$ItemExpiryDate = DateAdd("d" ,1, $SelectedDate);

echo "Current Date: " . ($curDate), "n";
echo "n";


echo "Item Expiry Date: " . date("d/m/Y", $ItemExpiryDate), "n";
echo "n";
?>

<?php

if ($curDate <= $ItemExpiryDate)
{
echo "1";
}
else
{
echo "2";
}
?>

Reply With Quote
  #2  
Old August 11th, 2002, 02:22 PM
Taoism Taoism is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Winnipeg, MB, Canada
Posts: 81 Taoism User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 sec
Reputation Power: 2
RE: Date Comparison problem

I believe the answer is in the last code block:
php Code:
Original - php Code
  1.  
  2. <?php
  3. function DateAdd($interval,$number,$date){
  4.  
  5.   $date_time_array = getdate($date);
  6.  
  7.   $hours = $date_time_array["hours"];
  8.   $minutes = $date_time_array["minutes"];
  9.   $seconds = $date_time_array["seconds"];
  10.   $month = $date_time_array["mon"];
  11.   $day = $date_time_array["mday"];
  12.   $year = $date_time_array["year"];
  13.  
  14.   switch($interval){
  15.     case "yyyy":
  16.       $year +=$number;
  17.       break;
  18.     case "q":
  19.       $year +=($number*3);
  20.       break;
  21.     case "m":
  22.       $month +=$number;
  23.       break;
  24.     case "y":
  25.     case "d":
  26.     case "w":
  27.       $day+=$number;
  28.       break;
  29.     case "ww":
  30.       $day+=($number*7);
  31.       break;
  32.     case "h":
  33.       $hours+=$number;
  34.       break;
  35.     case "n":
  36.       $minutes+=$number;
  37.       break;
  38.     case "s":
  39.       $seconds+=$number;
  40.       break;   
  41.   }
  42.   $timestamp=mktime($hours,$minutes,$seconds,$month$day,$year);
  43.   return $timestamp;
  44. }
  45. ?>
  46.  
  47. <?php
  48. $curDate=date("d/m/Y");
  49. $SelectedDate=$mine=strtotime("10 July 2000");
  50. echo "Selected Expiry Date: ".date("d/m/Y",$SelectedDate),"n";
  51. $ItemExpiryDate=DateAdd("d",1,$SelectedDate);
  52.  
  53. echo "Current Date: ".$curDate."n";
  54. echo "n";
  55.  
  56.  
  57. echo "Item Expiry Date: ".date("d/m/Y", $ItemExpiryDate)."n";
  58. echo "n";
  59. ?>
  60.  
  61. <?php
  62. // look at what you are comparing....
  63. // $curDate=date("d/m/Y");
  64. // and
  65. // return $timestamp;
  66. // i.e., a string representation of a date, and an integer representation
  67. // so you should expect buggy results...
  68. if($curDate<=$ItemExpiryDate){
  69.   echo "1";
  70. }else{
  71.   echo "2";
  72. }
  73. ?>


Cheers,
Keith.

Reply With Quote
  #3  
Old August 11th, 2002, 06:29 PM
postalcow postalcow is offline
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Ford CIty, PA USA
Posts: 1,267 postalcow User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
Send a message via Yahoo to postalcow
RE: Date Comparison problem

You know. I think you could make everything easier if you would base all dates on the epoch

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > Date Comparison problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway