jamesxg1 Posted September 19, 2012 Share Posted September 19, 2012 Hello fellow PHPFreaks, It's been a while! I've come across an issue, I'm trying to calculate the amount of time difference between 2 times. I have done the date side of things and I only need to compare times if the date matches the same date in the database but I'm having some trouble with the time side. This is what I was using (it worked fine for a day and I've just checked it and its now not working). $KickOff = (string) do_shortcode('[tb_match id=' . $post->ID . ']'); $KickOffMatches = array(); preg_match('/\<div class="date"\>(.*?)<\/div\>\<time\>(.*?)\<\/time\>/', $KickOff, $KickOffMatches); $FormattedDate = date('Y-m-d', strtotime($KickOffMatches[1])); $FormattedTime = date('H:i A', strtotime($KickOffMatches[2])); $AMPM = date('A', strtotime($KickOffMatches[2])); $RawDate = strtotime($FormattedDate); $RawTime = strtotime(date('H:i', strtotime($KickOffMatches[2]))); $start_date = new DateTime(date('H:i A')); $TD = $start_date->diff(new DateTime($KickOffMatches[2])); The area that is causing the issue is $start_date = new DateTime(date('H:i A')); $TD = $start_date->diff(new DateTime($KickOffMatches[2])); I've tried everything but all I'm getting is a fatal error. I was wondering if anyone here knew of a way that I could do this without using DateTime? Any help with this would be greatly appreciated! Many thanks, James. Quote Link to comment https://forums.phpfreaks.com/topic/268557-calculating-time-difference-error/ Share on other sites More sharing options...
premiso Posted September 19, 2012 Share Posted September 19, 2012 I've tried everything but all I'm getting is a fatal error. I was wondering if anyone here knew of a way that I could do this without using DateTime? It would help to post the fatal error message. Quote Link to comment https://forums.phpfreaks.com/topic/268557-calculating-time-difference-error/#findComment-1379268 Share on other sites More sharing options...
jamesxg1 Posted September 19, 2012 Author Share Posted September 19, 2012 I would have but where it was causing a serious obstruction on the site, I've had to remove it and use a series of IF statements to do the validations for the meantime. Sorry Premiso :S James. Quote Link to comment https://forums.phpfreaks.com/topic/268557-calculating-time-difference-error/#findComment-1379271 Share on other sites More sharing options...
premiso Posted September 19, 2012 Share Posted September 19, 2012 I would have but where it was causing a serious obstruction on the site Well without that we are spinning our wheels trying to assist you. You do know that the errors are generally logged to files as well. Depending on the WebServer, it should be in something like /var/log/apache2/error.log or /var/log/nginx/error.log or some form of that. Quote Link to comment https://forums.phpfreaks.com/topic/268557-calculating-time-difference-error/#findComment-1379274 Share on other sites More sharing options...
Jessica Posted September 19, 2012 Share Posted September 19, 2012 You should have a dev version of your site where you can put the problematic code and debug it. Quote Link to comment https://forums.phpfreaks.com/topic/268557-calculating-time-difference-error/#findComment-1379275 Share on other sites More sharing options...
jamesxg1 Posted September 19, 2012 Author Share Posted September 19, 2012 I would have but where it was causing a serious obstruction on the site Well without that we are spinning our wheels trying to assist you. You do know that the errors are generally logged to files as well. Depending on the WebServer, it should be in something like /var/log/apache2/error.log or /var/log/nginx/error.log or some form of that. Well, the info I do have is this: Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string but that is only a portion of the error to be fair. Thanks, James. Quote Link to comment https://forums.phpfreaks.com/topic/268557-calculating-time-difference-error/#findComment-1379278 Share on other sites More sharing options...
premiso Posted September 19, 2012 Share Posted September 19, 2012 You should have a dev version of your site where you can put the problematic code and debug it. Quote Link to comment https://forums.phpfreaks.com/topic/268557-calculating-time-difference-error/#findComment-1379279 Share on other sites More sharing options...
jamesxg1 Posted September 19, 2012 Author Share Posted September 19, 2012 You should have a dev version of your site where you can put the problematic code and debug it. I usually do for my sites, but this is a friends site and I'm helping them out. Quote Link to comment https://forums.phpfreaks.com/topic/268557-calculating-time-difference-error/#findComment-1379280 Share on other sites More sharing options...
premiso Posted September 19, 2012 Share Posted September 19, 2012 Ok this is just weird: $RawTime = strtotime(date('H:i', strtotime($KickOffMatches[2]))); Why are you strtotime, then date, then strtotime again? However, ignoring the confusion there, your problem is that DateTime expects a timestamp, not a date modified format (unless you do createFromFormat). $start_date = new DateTime(time()); $TD = $start_date->diff(new DateTime($KickOffMatches[2])); Should fix the exception. Quote Link to comment https://forums.phpfreaks.com/topic/268557-calculating-time-difference-error/#findComment-1379282 Share on other sites More sharing options...
jamesxg1 Posted September 19, 2012 Author Share Posted September 19, 2012 $KickOffMatches[2] needed to be rendered into a usable format, so I've used date to format it using a "stamp" of the unusable date format then I've strtotime the correctly formatted date. Quote Link to comment https://forums.phpfreaks.com/topic/268557-calculating-time-difference-error/#findComment-1379285 Share on other sites More sharing options...
jamesxg1 Posted September 19, 2012 Author Share Posted September 19, 2012 Ok this is just weird: $RawTime = strtotime(date('H:i', strtotime($KickOffMatches[2]))); Why are you strtotime, then date, then strtotime again? However, ignoring the confusion there, your problem is that DateTime expects a timestamp, not a date modified format (unless you do createFromFormat). $start_date = new DateTime(time()); $TD = $start_date->diff(new DateTime($KickOffMatches[2])); Should fix the exception. Ok no worries, I'll give that a shot in a moment Thanks premiso. Quote Link to comment https://forums.phpfreaks.com/topic/268557-calculating-time-difference-error/#findComment-1379287 Share on other sites More sharing options...
jamesxg1 Posted September 19, 2012 Author Share Posted September 19, 2012 Ok this is just weird: $RawTime = strtotime(date('H:i', strtotime($KickOffMatches[2]))); Why are you strtotime, then date, then strtotime again? However, ignoring the confusion there, your problem is that DateTime expects a timestamp, not a date modified format (unless you do createFromFormat). $start_date = new DateTime(time()); $TD = $start_date->diff(new DateTime($KickOffMatches[2])); Should fix the exception. Returns the following: Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (1348070008) at position 8 (0): Unexpected character' in /home/sites/colewebdesigns.co.uk/public_html/wp-content/themes/footballclub-2.1/predictions.php:66 Stack trace: #0 /home/sites/colewebdesigns.co.uk/public_html/wp-content/themes/footballclub-2.1/predictions.php(66): DateTime->__construct('1348070008') #1 /home/sites/colewebdesigns.co.uk/public_html/wp-content/themes/footballclub-2.1/loop-match.php(12): include('/home/sites/col...') #2 /home/sites/colewebdesigns.co.uk/public_html/wp-includes/template.php(392): require('/home/sites/col...') #3 /home/sites/colewebdesigns.co.uk/public_html/wp-includes/template.php(366): load_template('/home/sites/col...', false) #4 /home/sites/colewebdesigns.co.uk/public_html/wp-includes/general-template.php(128): locate_template(Array, true, false) #5 /home/sites/colewebdesigns.co.uk/public_html/wp-content/themes/footballclub-2.1/single.php(23): get_template_part('loop', 'matc in /home/sites/colewebdesigns.co.uk/public_html/wp-content/themes/footballclub-2.1/predictions.php on line 66 Quote Link to comment https://forums.phpfreaks.com/topic/268557-calculating-time-difference-error/#findComment-1379288 Share on other sites More sharing options...
premiso Posted September 19, 2012 Share Posted September 19, 2012 Grasping at straws, but maybe this: $start_date = new DateTime(); $TD = $start_date->diff(new DateTime(strtotime($KickOffMatches[2]))); Quote Link to comment https://forums.phpfreaks.com/topic/268557-calculating-time-difference-error/#findComment-1379289 Share on other sites More sharing options...
jamesxg1 Posted September 19, 2012 Author Share Posted September 19, 2012 Got the same again :S Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (1348068720) at position 7 (7): Unexpected character' in /home/sites/colewebdesigns.co.uk/public_html/wp-content/themes/footballclub-2.1/predictions.php:67 Stack trace: #0 /home/sites/colewebdesigns.co.uk/public_html/wp-content/themes/footballclub-2.1/predictions.php(67): DateTime->__construct('1348068720') #1 /home/sites/colewebdesigns.co.uk/public_html/wp-content/themes/footballclub-2.1/loop-match.php(12): include('/home/sites/col...') #2 /home/sites/colewebdesigns.co.uk/public_html/wp-includes/template.php(392): require('/home/sites/col...') #3 /home/sites/colewebdesigns.co.uk/public_html/wp-includes/template.php(366): load_template('/home/sites/col...', false) #4 /home/sites/colewebdesigns.co.uk/public_html/wp-includes/general-template.php(128): locate_template(Array, true, false) #5 /home/sites/colewebdesigns.co.uk/public_html/wp-content/themes/footballclub-2.1/single.php(23): get_template_part('loop', 'matc in /home/sites/colewebdesigns.co.uk/public_html/wp-content/themes/footballclub-2.1/predictions.php on line 67 Quote Link to comment https://forums.phpfreaks.com/topic/268557-calculating-time-difference-error/#findComment-1379296 Share on other sites More sharing options...
kicken Posted September 19, 2012 Share Posted September 19, 2012 DateTime constructor does take a time string, same as strtotime() more or less. To use a timestamp you'd need to pass the string '@1234567'. What kind of a value does $KickOffMatches[2] contain? Quote Link to comment https://forums.phpfreaks.com/topic/268557-calculating-time-difference-error/#findComment-1379297 Share on other sites More sharing options...
jamesxg1 Posted September 19, 2012 Author Share Posted September 19, 2012 DateTime constructor does take a time string, same as strtotime() more or less. To use a timestamp you'd need to pass the string '@1234567'. What kind of a value does $KickOffMatches[2] contain? It would usually contain something like 16:32 pm or 4:32 am for instance Quote Link to comment https://forums.phpfreaks.com/topic/268557-calculating-time-difference-error/#findComment-1379299 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.