Author Topic: [SOLVED] Date Comparison Failing.  (Read 212 times)

0 Members and 1 Guest are viewing this topic.

Offline hellonokoTopic starter

  • Enthusiast
  • Posts: 269
    • View Profile
[SOLVED] Date Comparison Failing.
« on: November 19, 2008, 01:22:47 PM »
Code: [Select]
echo $daystillupload = date('Y-m-d') - "2008-11-18";

Is my code to compare the current date with the last login date and display how many days since a person last logged in. This should return 1. But returns 0? What am I doing wrong.

Thanks,
ian

Offline Mark Baker

  • Addict
  • Posts: 1,586
  • Gender: Male
    • View Profile
Re: Date Comparison Failing.
« Reply #1 on: November 19, 2008, 01:28:36 PM »
Try doing things the other way round, converting the human-readable dates to PHP date/time stamps, then doing the mathematics
9 out of 10 PHP problems can be resolved by setting
Code: (php) [Select]
error_reporting(E_ALL);
ini_set('display_errors', 1);
php -l <filename> will identify 9 out of the remaining 10 problems
Remember, the command line is your friend
Development Projects: PHPExcel and PHPPowerPoint

Offline hellonokoTopic starter

  • Enthusiast
  • Posts: 269
    • View Profile
Re: Date Comparison Failing.
« Reply #2 on: November 19, 2008, 01:35:13 PM »
Tried:

Code: [Select]
$daystillupload = strtotime(date('Y-m-d')) - strtotime($lastuploaddate);

And:

Code: [Select]
$daystillupload = date('Y-m-d') - strtotime($lastuploaddate);

Both return: 1226989592

$lastuploaddate contains 2008-11-18


Offline premiso

  • Karma Chameleon
  • Staff Alumni
  • Freak!
  • *
  • Posts: 6,671
  • Gender: Female
  • effing right
    • View Profile
    • PHP Help
Re: Date Comparison Failing.
« Reply #3 on: November 19, 2008, 02:10:14 PM »
They return what is expected the timestamp.

try this:



$daystillupload 
time() - strtotime($lastuploaddate); // just use time

echo date('Y-m-d'$daystillupload); // use date here for displaying.

Offline hellonokoTopic starter

  • Enthusiast
  • Posts: 269
    • View Profile
Re: Date Comparison Failing.
« Reply #4 on: November 19, 2008, 02:12:42 PM »
I found this solution that seems to work well.
Code: [Select]
function compareDates($date1,$date2)
{
$date1_array = explode("-",$date1);
$date2_array = explode("-",$date2);

$timestamp1 = mktime(0,0,0,$date1_array[1],$date1_array[2],$date1_array[0]);
$timestamp2 = mktime(0,0,0,$date2_array[1],$date2_array[2],$date2_array[0]);

return ($timestamp1 - $timestamp2);


}

$daystillupload = compareDates( $date = date('Y-m-d'), $lastuploaddate) / 86400;