3s2ng Posted March 18, 2008 Share Posted March 18, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/96654-ebay-time/ Share on other sites More sharing options...
Jeremysr Posted March 18, 2008 Share Posted March 18, 2008 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'; Quote Link to comment https://forums.phpfreaks.com/topic/96654-ebay-time/#findComment-494620 Share on other sites More sharing options...
3s2ng Posted March 19, 2008 Author Share Posted March 19, 2008 I cant get it working. Quote Link to comment https://forums.phpfreaks.com/topic/96654-ebay-time/#findComment-496362 Share on other sites More sharing options...
cunoodle2 Posted March 20, 2008 Share Posted March 20, 2008 I cant get it working. This does us absolutely no good of any kind. Post your code. What's is doing? What error messages are you getting? Your saying "I can't get it working." would be just as useful as me saying to you "Well keep trying." Quote Link to comment https://forums.phpfreaks.com/topic/96654-ebay-time/#findComment-496437 Share on other sites More sharing options...
Jeremysr Posted March 20, 2008 Share Posted March 20, 2008 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" Quote Link to comment https://forums.phpfreaks.com/topic/96654-ebay-time/#findComment-496475 Share on other sites More sharing options...
dwest Posted March 20, 2008 Share Posted March 20, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/96654-ebay-time/#findComment-496480 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.