Hi guys,

I am working on my PHP script as I want to add the value in the variable to add it to 5 hours forward.

When I use this:

$links = $row['links'];
$html = file_get_html($links);

$base = $row['links'];

$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_URL, $base);
curl_setopt($curl, CURLOPT_REFERER, $base);
$str = curl_exec($curl);

// Create a DOM object
$html = new simple_html_dom();
// Load HTML from a string

$title1 = $html->find('li[id=row1-1]', 0)->plaintext; // with this
$title1 = preg_split("/ {6,}/",$title1);

$time1 = $title1[1];

echo '<span id="time1">'.$time1.'</span> - <span id="title1">'.$programme1. $programme1_bbf. $programme1_cat.'</span><br></br>';

It will show this as the output:

5:00 PM

I want to add the hours in the variable of time1 to add it to 5 hours forward, so it would show like this:

10:00 PM

Can you please show me an example of how I can add the value in the variable to add it to 5 hours forward?

Recommended Answers

All 4 Replies


$timeInString = "5:00 PM";
// @see http://php.net/manual/en/function.strtotime.php
$timeStamp = strtotime($timeInString);
// @see http://php.net/manual/en/datetime.construct.php
$dateTime = new DateTime();
// @see http://php.net/manual/en/datetime.settimestamp.php
// @see http://php.net/manual/en/dateinterval.construct.php
$dateInterval = new DateInterval("PT5H");
// @see  http://php.net/manual/en/datetime.add.php
// @see http://php.net/manual/en/function.date.php
$result = $dateTime->format("g:i A");

echo $result;  
//10:00 PM
commented: +1 +13


use DateTime and DateInterval, like this:


    $time = '05:00 PM';
    $dt = (new Datetime($time))->add(new DateInterval('PT5H'))->format('g:i A');

    echo $dt; # outputs 10:00 PM

For more information check the documentation.

Docs about DateTime:

Docs about DateInterval:

@jkon sorry, just saw your answer!

thanks for your help guys, problem are now solved!

You're welcome mark103 , just mark it as solved. (cereal no problem , there are several times that I response the same time as others)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.