Jump to content

Time Zone Setting!


sohaibshaheen

Recommended Posts

I am sorry I have absolutely no idea where this question fits in this forum bcoz its related to PHP , MySQL and Javascript...  :-\

 

So here is my question ..

 

I use MySql database to store posts.. where 'postdate and time' is saved using  'CURRENT_TIMESTAMP' of MySql that offcourse gives "2010-08-31 09:47:10" format...

 

When I have to calculate when the post was posted just before loading the posts on users wall e.g.

 

"2 min ago"...

 

I take the 'postdate and time' from database then use strtotime() function in php to convert it to string you know since jan 1 1970.

 

Now this time stamp is sent to javascript function which uses 'Date' Object to create current time and then 'getTime' to get the string like one Mentioned earlier. it then calculates the difference and displays it..

 

Just to inform you.. I used javascript to I may refresh "posted since" periodically using "SetInterval()"

 

But the problem here arises. There is difference of 1 Hour and 24 Sec in both times.......

 

:confused: Sorry If I confused you... But the problem is simple. In javascript date object and MySql  --> strtotime()

there is time difference...

 

How I can correct it ???

 

 

Plz Help ...

 

Codes:

 

1. function to convert str to time


function change($date){

$time_early=strtotime($date);

return $time_early;


}


 

 

2. javascript Code to check "Post Since"

 


function calc_difference() {

$("[name=posttime]").each(function() {

 var current=$(this).attr("id");
  localtimevalue = new Date();
d = localtimevalue.getTime();	

         

 var d= parseInt(d)/1000;	
 var diff=d-parseInt(current);	 	

 var diff=parseInt(diff);
 var min=parseInt(diff/60);	        
 var hours=parseInt(diff/3600);
var days=parseInt(diff/86400);	
var months=parseInt(diff/2592000);	
var years=parseInt(diff/31104000);	


if( min==0 ){ 	


$(this).text("Posted: "+diff +" seconds ago ");

	}	



if(min>0 && hours==0 && days==0 && months==0){ 	
		if(min==1){
					 $(this).text("Posted:  a minute ago ");
 	   			  
		}else{

					$(this).text("Posted: "+min +" minutes ago ");
		}	

	}

if(min>0 && hours>0 && days==0 && months==0){ 	

		if(hours==1){
					 $(this).text("Posted:  an hour ago ");
 	   			  
		}else{

					$(this).text("Posted: "+hours +" hours ago ");
		}	



	}

if(days>0 && months==0 && years==0){ 	


		if(days==1){
					 $(this).text("Posted:  a day ago ");
 	   			  
		}else{

					$(this).text("Posted: "+days +" days ago ");
		}	



	}



if(months>0 && years==0 ){ 	


		if(months==1){
					 $(this).text("Posted:  a month ago ");
 	   			  
		}else{

					$(this).text("Posted: "+months +" months ago ");
		}	



	}	


if(years>0){ 	


		if(years==1){
					 $(this).text("Posted:  a year ago ");
 	   			  
		}else{

					$(this).text("Posted: "+years +" years ago ");
		}	



	}	








  });


   
}





 

Javascript function is called on load and Jquery's .each function is used to update all post's time.

 

 

 

Link to comment
Share on other sites

I know this doesn't answer your exact question, but at a glance it seems you're going about this all wrong. Rather than taking the timestamp out of MySQL, then parsing it in PHP, then parsing it again in JavaScript, maybe you could simply use MySQL's time functions?

 

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_timestampdiff

 

Between that and TIME_FORMAT() you could easily just have MySQL spit out "2 min ago" directly to your SQL result. This could really help to clean up and uncomplicate this part of your code. If after looking at the MySQL documentation you're unclear on how those functions work I can help further. They're worth taking the time to learn because they're invaluable for these types of tasks which come up often. You can also shift time zones right in your SQL.

Link to comment
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.