Jump to content

Recommended Posts

I'm trying to make a countdown page by getting the information from this websites api but I need to know how I would get it to display how many hours and minutes left. The URL is http://services.tvrage.com/tools/quickinfo.php?show=dexter&exact=1

 

The part I'm working on is:

 

RFC3339@2009-12-06T21:00:00

 

Does anyone know how?

Link to comment
https://forums.phpfreaks.com/topic/183519-making-a-countdown-from-websites-api/
Share on other sites

Assuming your time strings will always be of that format (and have a valid time) You could do something like this

$string = "RFC3339@2009-12-06T21:00:00";

$time = end(explode('@', $string));
$time = strtotime($time);

$now = time();
$timeLeft = $time - $now;
$secsInDay = 60*60*24;//60 seconds in a min * 60 mins in an hour * 24 hours in a day
//day sleft
$daysLeft = (int)($timeLeft / $secsInDay);
$timeLeft %= $secsInDay;
$secsInHour = 60*60;
//hours left
$hoursLeft = (int)($timeLeft / $secsInHour);
$timeLeft %= $secsInHour;
//minutes left;
$minutesLeft = (int)($timeLeft / 60);//60 secs in a minute
$timeLeft %= 60;
//secs left is timeLeft
$secsLeft = $timeLeft;

echo "$daysLeft days $hoursLeft hours $minutesLeft minutes and $secsLeft seconds";

 

Just ask if you need an explanation

Thanks mikesta :)

 

I'm trying to list about 10 shows and I want to know if there is any better way in doing this? This is what I've got now:

 

<?php

function get_between ($text, $s1, $s2) {
   $mid_url = "";
   $pos_s = strpos($text,$s1);
   $pos_e = strpos($text,$s2);
   for ( $i=$pos_s+strlen($s1) ; ( ( $i < ($pos_e)) && $i < strlen($text) ) ; $i++ ) {
       $mid_url .= $text[$i];
   }
   return $mid_url;
}


function get_time ($string) {

$time = end(explode('@', $string));
$time = strtotime($time);

$now = time();
$timeLeft = $time - $now;
$secsInDay = 60*60*24;//60 seconds in a min * 60 mins in an hour * 24 hours in a day
//day sleft
$daysLeft = (int)($timeLeft / $secsInDay);
$timeLeft %= $secsInDay;
$secsInHour = 60*60;
//hours left
$hoursLeft = (int)($timeLeft / $secsInHour);
$timeLeft %= $secsInHour;
//minutes left;
$minutesLeft = (int)($timeLeft / 60);//60 secs in a minute
$timeLeft %= 60;
//secs left is timeLeft
$secsLeft = $timeLeft;

echo "$daysLeft days $hoursLeft hours $minutesLeft minutes and $secsLeft seconds<BR><BR>";

}

$rs2 = file_get_contents("http://services.tvrage.com/tools/quickinfo.php?show=lost&exact=1");
$between = get_between($rs2, "RFC3339", "GMT+0");
get_time($between);

$rs2 = file_get_contents("http://services.tvrage.com/tools/quickinfo.php?show=dexter&exact=1");
$between = get_between($rs2, "RFC3339", "GMT+0");
get_time($between);
?>

well this

for ( $i=$pos_s+strlen($s1) ; ( ( $i < ($pos_e)) && $i < strlen($text) ) ; $i++ ) {
       $mid_url .= $text[$i];
   }

 

can be accomplished with substr

 

<?php

function get_between ($text, $s1, $s2) {
   $pos_s = strpos($text,$s1);
   $pos_e = strpos($text,$s2);	
   return substr($text, $pos_s + strlen($s1), $pos_e - ($pos_s + strlen($s1)) );
}

$text = "This will be some VEERY long text with somsde words and more words, but yeah...";
echo get_between($text, "be", "words");


?>

some VEERY long text with somsde

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.