jarvis Posted August 24, 2022 Share Posted August 24, 2022 (edited) Good afternoon, I've got myself in a muddle over what I thought should be some simple code. Files are uploaded in a CMS and have a date field next to them (returning in format of d/m/Y). So when a new file is added, the user selects the day the file(s) should display For example: File A - 23/08/2022 File B - 24/08/2022 File C - 25/08/2022 Files for the previous day need to display on the current day until a set time (in this instance 13:59:59) I've setup some code as follows: $yesterday = new DateTime("yesterday 13:59:59", new DateTimeZone('Europe/London')); echo "Yesterday " . $yesterday->format('d/m/Y H:i:s') . "<br/>"; date_default_timezone_set('Europe/London'); echo "Today " . date("d/m/Y h:i:s") . "<br/>"; $tomorrow = new DateTime("tomorrow 00:00:01", new DateTimeZone('Europe/London')); echo "Tomorrow " . $tomorrow->format('d/m/Y H:i:s') . "<br/>"; So currently, this outputs: Yesterday 23/08/2022 13:59:59 Today 24/08/2022 02:37:08 Tomorrow 25/08/2022 00:00:01 The above I use to check the date/times whilst I play around with the code logic. I then loop through the uploaded files and dates from the CMS. In the loop, I grab the date (stored as d/m/Y) next to each file so therefore adjust it: $file_date = DateTime::createFromFormat('d/m/Y', $date); Then within the loop, I also add a check to display the file if its the right day/time range: #if today is greater than yesterday but less than tomorrow if($file_date->format('d/m/Y H:i:s') > $yesterday->format('d/m/Y H:i:s') && $file_date->format('d/m/Y H:i:s') < $tomorrow->format('d/m/Y H:i:s') ): //display the file URL endif; It then shows File A and File B but surely should only show File A as it's after Yesterday 23/08/2022 13:59:59 I think I've missed something really obvious but can't see for looking! Edited August 24, 2022 by jarvis Make the file_date var clearer to prevent confusion Quote Link to comment Share on other sites More sharing options...
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.