Jump to content

Recommended Posts

Hey all,

 

I'm playing around with some code, and basically the idea is:

 

  • Person changes their profile
     
  • I fetch some XML that has a unix timestamp for the time the person changed their profile, so it'll keep increasing everytime I fetch the XML. It looks like this: <profileTime>12086</profileTime>
     
  • I then run it through a function to convert the unix time to hours:mins:secs
     
  • Finally I run it through another function to calc how long in the past that was, so it can be displayed in a user friendly format.

 

The issue is, every time I run it through the second function, the final displayed time keeps increasing, rather than staying the same, which it should stay the same, because the person only changed their profile once, at that original time.

 

Here is the first function, where I convert the unix time to h:m:s:

 

function convert($sec, $padHours = false) {

    $hms = null;
    $hours = intval(intval($sec) / 3600);
    $hms .= ($padHours) ? str_pad($hours, 2, "0", STR_PAD_LEFT). ':' : $hours. ':';
    $minutes = intval(($sec / 60) % 60);
    $hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ':';
    $seconds = intval($sec % 60);
    $hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT);
    return $hms;
}

 

And here's the code in teh second function to format it:

 

function olddate($hours, $minutes, $seconds) {

	/* List of working timezones here:
	*
	*  http://www.php.net/manual/en/timezones.php
	*/
	date_default_timezone_set('America/Chicago');

	// Calculate the exact day and time the status message was set
	$pf_time = strtotime("-".$hours." hours ".$minutes." minutes ".$seconds." seconds");
	echo date("D F j, Y, g:i (s) a", $pf_time);
	//return date("D F j, Y, g:i (s) a", $pf_time);
}

 

Anyone able to offer any advice?

Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/180711-strtotime-issue/
Share on other sites

Why don't use just use date

 

ie

$time = 12086;
date_default_timezone_set('UTC');
echo date("H:i:s",$time);

 

I'm using Date at the bottom of the second function, but I need to calc how long ago in the past it was first. I use date to format the results.

 

The only thing I'm thinking is possibly the conversion in the first function is causing the problem?

 

Link to comment
https://forums.phpfreaks.com/topic/180711-strtotime-issue/#findComment-953981
Share on other sites

can you give me an example of what your trying to do

 

Do you want me to send you the actual php file? Other than having the actual php file, the code in the original post is about all there is to it, as far as the problem functions go. I can email you the file itself if you want to pm me an email address.

 

 

Link to comment
https://forums.phpfreaks.com/topic/180711-strtotime-issue/#findComment-954442
Share on other sites

No, I mean can you give me an example of what it suppose to do,

 

basically this part id unclear

Finally I run it through another function to calc how long in the past that was, so it can be displayed in a user friendly format.

The problem is 12086 is a time stamp, not a date stamp.. so are you only working on a day by day basis ?

if that's true then you could do this:~

date_default_timezone_set('UTC');
echo date("H:i:s",time()-$time);

 

but i think i am missing something!

Link to comment
https://forums.phpfreaks.com/topic/180711-strtotime-issue/#findComment-954502
Share on other sites

Hmmm, ok. I am fetching an Aol instant messenger XML file, with a list of screen names in the XML, with each one's specific info in the nodes.

 

When any of the screen names changes their Status Message, AIM sets a unix timestamp on it, so I would assume, that even though the timestamp will grow in number (due to the passage of time since it's been set), if I do a calculation using strtotime, to figure out how far in the past it calculates to, it should always show the exact same time/date it was set, until the user changes their Status Message again.

 

My problem so far is: I run it through 2 separate functions, the first, converts the Unix Timestamp into hours, minutes and seconds, returning a format like this: 12:35:45

 

After I get those numbers back, I send it through the second function with those numbers ($hours = 12, $minutes = 35, $seconds = 45)

 

Then I;m using the strtotime to subtract that amount of time from the current timestamp, and finally, using Date to show it in a more user friendly format, like: Status Message Set on: Thu Dec 12, 2009, 12:35 (45) pm

 

It seems to set to correct time after running through both functions the very first run-through, however, any refresh of the page will add however many seconds, minutes, etc. that have passed since I ran it through the original time, rather than staticly show the same exact timestamp no matter how many refreshes.

 

So to sum up, basically, if I'm on AIM and change my status message, no matte rhow much time passes, my timestamp will always remain the same until I change it again. My code isn't keeping it set to the same time, it keeps increasing as I hit refresh for some reason.

 

Link to comment
https://forums.phpfreaks.com/topic/180711-strtotime-issue/#findComment-954554
Share on other sites

I would assume that the time would from AOL would be the amount of time they have been away for.

1 = 1 second

120 = 2 minutes etc.

in that case, you could only really get a true time by pulling the xml every few minutes to check if they are still away

Link to comment
https://forums.phpfreaks.com/topic/180711-strtotime-issue/#findComment-954559
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.