Jump to content

Calculating time difference, error.


jamesxg1

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.