hassank1 Posted July 20, 2008 Share Posted July 20, 2008 I've a variable $PostDate .. which I get from a datetime mysql field I want to check if this date is today so I echo "today" or if it's yesterday I echo "yesterday" Link to comment https://forums.phpfreaks.com/topic/115678-solved-if-date-today/ Share on other sites More sharing options...
Guest Xanza Posted July 20, 2008 Share Posted July 20, 2008 It would depend on the format of $PostDate... For example, if $PostDate shows the date as: "Weekday, Month date" (Example: Monday, April 25) then this would work: <?php if($PostDate == date("l, F d")){ echo "Today"; } if($PostDate == date("l, F d")-1){ echo "Yesterday"; } ?> This should work, depending how your initial SQL variable is output, just change date() to suit your needs. Hope this helped! Link to comment https://forums.phpfreaks.com/topic/115678-solved-if-date-today/#findComment-594705 Share on other sites More sharing options...
hassank1 Posted July 20, 2008 Author Share Posted July 20, 2008 Thx , I'll check it .. btw this is an example of a $postdate output : 2008-04-18 04:38:08 Link to comment https://forums.phpfreaks.com/topic/115678-solved-if-date-today/#findComment-594707 Share on other sites More sharing options...
Guest Xanza Posted July 20, 2008 Share Posted July 20, 2008 Then make the date() function match your SQL output, use php.net to help you: http://us2.php.net/manual/en/function.date.php Link to comment https://forums.phpfreaks.com/topic/115678-solved-if-date-today/#findComment-594714 Share on other sites More sharing options...
hassank1 Posted July 20, 2008 Author Share Posted July 20, 2008 I've used your code to implement these functions .. and everything worked just fine function isToday($date) //check if it's today { $date = date("l, F d",strtotime($date)); if($date == date("l, F d")) return true; else return false; } function isYesterday($date) //check if it's yesterday { $date = date("l, F d",strtotime($date)); if($date == date("l, F d")-1) return true; else return false; } Link to comment https://forums.phpfreaks.com/topic/115678-solved-if-date-today/#findComment-594715 Share on other sites More sharing options...
Bendude14 Posted July 20, 2008 Share Posted July 20, 2008 Instead of using DateTime in my sql use timestamp i believe it is twice as fast...and your code will still work as it returns in the same format Link to comment https://forums.phpfreaks.com/topic/115678-solved-if-date-today/#findComment-594716 Share on other sites More sharing options...
hassank1 Posted July 20, 2008 Author Share Posted July 20, 2008 OK .. thx for the advice . Link to comment https://forums.phpfreaks.com/topic/115678-solved-if-date-today/#findComment-594717 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.