hellonoko Posted November 19, 2008 Share Posted November 19, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/133377-solved-date-comparison-failing/ Share on other sites More sharing options...
Mark Baker Posted November 19, 2008 Share Posted November 19, 2008 Try doing things the other way round, converting the human-readable dates to PHP date/time stamps, then doing the mathematics Quote Link to comment https://forums.phpfreaks.com/topic/133377-solved-date-comparison-failing/#findComment-693705 Share on other sites More sharing options...
hellonoko Posted November 19, 2008 Author Share Posted November 19, 2008 Tried: $daystillupload = strtotime(date('Y-m-d')) - strtotime($lastuploaddate); And: $daystillupload = date('Y-m-d') - strtotime($lastuploaddate); Both return: 1226989592 $lastuploaddate contains 2008-11-18 Quote Link to comment https://forums.phpfreaks.com/topic/133377-solved-date-comparison-failing/#findComment-693713 Share on other sites More sharing options...
premiso Posted November 19, 2008 Share Posted November 19, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/133377-solved-date-comparison-failing/#findComment-693733 Share on other sites More sharing options...
hellonoko Posted November 19, 2008 Author Share Posted November 19, 2008 I found this solution that seems to work well. 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; Quote Link to comment https://forums.phpfreaks.com/topic/133377-solved-date-comparison-failing/#findComment-693735 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.