Jump to content

Recommended Posts

Hi Freaks,

 

I need some help.

 

Currently I'm working with eBay API and noticed that the item end time is using ISO 8601 format something like "2008-10-31T00:48:06.000Z"

 

now I want to get the time Remaining and to get this I want to get the time difference of the current date/time against the item end date which is the ISO 8601 format.

 

The result I want is something like "1D 23H 12M 1S"

 

I hope somebody can help me.

 

THanks,

Mark

Link to comment
https://forums.phpfreaks.com/topic/96654-ebay-time/
Share on other sites

I would get the different parts of the date using a bunch of substr()'s...

 

$endtime = "2008-10-31T00:48:06.000Z";

$year = substr($endtime, 0, 4);
$month = substr($endtime, 5, 2);
$day = substr($endtime, 8, 2);
// etc...

 

...make a UNIX timestamp out of all those variables, subtracting it from the current time...

 

$endtime_timestamp = mktime($hour, $minute, $second, $month, $day, $year);
$seconds_left = time() - $endtime_timestamp;

 

...and use getdate() to find out how many days, hours, etc. are left.

 

$time_left = getdate($seconds_left);
$time_left_string = $time_left['yday'].'D '.$time_left['hours'].'H '.$time_left['minutes'].'M '.$time_left['seconds'].'S';

Link to comment
https://forums.phpfreaks.com/topic/96654-ebay-time/#findComment-494620
Share on other sites

Ok, I did make one mistake, the time() should have been subtracted from $endtime_timestamp instead of the other way around. Here is a function which I've tested:

 

function time_left($endtime) {
$year = substr($endtime, 0, 4);
$month = substr($endtime, 5, 2);
$day = substr($endtime, 8, 2);
$hour = substr($endtime, 11, 2);
$minute = substr($endtime, 14, 2);
$second = substr($endtime, 17, 2);

$endtime_timestamp = mktime($hour, $minute, $second, $month, $day, $year);
$seconds_left = $endtime_timestamp - time();

$time_left = getdate($seconds_left);
$time_left_string = $time_left['yday'].'D '.$time_left['hours'].'H '.$time_left['minutes'].'M '.$time_left['seconds'].'S';

return $time_left_string;
}

echo time_left("2008-03-21T00:48:06.000Z"); // Echos "224D 22H 44M 5S"

Link to comment
https://forums.phpfreaks.com/topic/96654-ebay-time/#findComment-496475
Share on other sites

I just did this myself with the help of the fine folks here.

First, eBay supplies time left as TimeLeft in their getsingleitem call. But it is indeed in the standardized format. This reformats it:

 

		$timeLeft = $res->Item->TimeLeft;
	$x =array(1 => 'Day', 'Hour', 'Minute', 'Second');
	//escaped backslashes are for the Wordpress quicktag which removes backslashes upon insert.
	preg_match('/\\AP(?\\d*)D)?T(?\\d*)H)?(?\\d*)M)?(\\d*)S\\z/x',$timeLeft, $d);
	for ($i = 1; $i < 5; $i++){
		$dare .= $d[$i] ? $d[$i].' '.$x[$i].($d[$i] > 1 ? 's ' : ' ') :'';
	}

 

BTW I'm using the PHP kit from eBay which has a complete package of the API wrapped in PHP.

 

Example in use here:

www.smallartgems.com

Link to comment
https://forums.phpfreaks.com/topic/96654-ebay-time/#findComment-496480
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.