Jump to content

How would you use this timestamp code?


man5

Recommended Posts

This code is taken from http://itfeast.blogspot.in/2013/08/php-convert-timestamp-into-facebook.html

How would you actually show this on a page? Say I have a mysql table with date column and I would like to insert the results of this code in there and as well as show it on page, how would that be done?

$today = time();    
                 $createdday= strtotime($post['created']); //mysql timestamp of when post was created  
                 $datediff = abs($today - $createdday);  
                 $difftext="";  
                 $years = floor($datediff / (365*60*60*24));  
                 $months = floor(($datediff - $years * 365*60*60*24) / (30*60*60*24));  
                 $days = floor(($datediff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));  
                 $hours= floor($datediff/3600);  
                 $minutes= floor($datediff/60);  
                 $seconds= floor($datediff);  
                 //year checker  
                 if($difftext=="")  
                 {  
                   if($years>1)  
                    $difftext=$years." years ago";  
                   elseif($years==1)  
                    $difftext=$years." year ago";  
                 }  
                 //month checker  
                 if($difftext=="")  
                 {  
                    if($months>1)  
                    $difftext=$months." months ago";  
                    elseif($months==1)  
                    $difftext=$months." month ago";  
                 }  
                 //month checker  
                 if($difftext=="")  
                 {  
                    if($days>1)  
                    $difftext=$days." days ago";  
                    elseif($days==1)  
                    $difftext=$days." day ago";  
                 }  
                 //hour checker  
                 if($difftext=="")  
                 {  
                    if($hours>1)  
                    $difftext=$hours." hours ago";  
                    elseif($hours==1)  
                    $difftext=$hours." hour ago";  
                 }  
                 //minutes checker  
                 if($difftext=="")  
                 {  
                    if($minutes>1)  
                    $difftext=$minutes." minutes ago";  
                    elseif($minutes==1)  
                    $difftext=$minutes." minute ago";  
                 }  
                 //seconds checker  
                 if($difftext=="")  
                 {  
                    if($seconds>1)  
                    $difftext=$seconds." seconds ago";  
                    elseif($seconds==1)  
                    $difftext=$seconds." second ago";  
                 }  
                 echo " | ".$difftext;  
Link to comment
Share on other sites

Not 100% sure on the question, but I'll give it a crack;

 

Variables look like: $(Name) to assign value to a variable you place = behind it and the value; If you want to print it out put echo/print in front of it.

 

Below are a list of the variables in the code, you can echo/print any of them out, if you place the echo after the IF statement with the variable condition then you will get your results from that IF Statement. If you place your echo before the IF Statement then you will get the results before the IF Statement has changed it.

$today = time();    
                 $createdday= strtotime($post['created']); //mysql timestamp of when post was created  
                 $datediff = abs($today - $createdday);  
                 $difftext="";  
                 $years = floor($datediff / (365*60*60*24));  
                 $months = floor(($datediff - $years * 365*60*60*24) / (30*60*60*24));  
                 $days = floor(($datediff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));  
                 $hours= floor($datediff/3600);  
                 $minutes= floor($datediff/60);  
                 $seconds= floor($datediff);  

Hope this helps answer the question. If that was the question.

Link to comment
Share on other sites

Adition if you want to insert into a database, you would build a string with the variables from above ie

 

Example only

$MyTime = "$days | $hours | $months | $years | etc";

With your $MyTime variable now, you drop it into your INSERT Statement for your database;

INSERT INTO MYDB (MyName, TimeStamp) VALUES ('AName', '$MyTime');
Link to comment
Share on other sites

Things are still little hazy. Let me try it my method based on yours.

$Mytime = '$createdday |  $datediff | $difftext | seconds | $minutes | $days | $hours | $months | $years';

$today = time($Mytime);


//pardon the mysql_* query

$insert  =  mysql_query("INSERT INTO posts (created)  VALUES($today)");


also, I am a little lost on this variable. What's it for?

$createdday= strtotime($post['created']);
Link to comment
Share on other sites

Actually you are right. I understand your code now.  Now I still get an error but it's because the following variable isn't defined.

$createdday= strtotime($post['created']);

where do I define the $post field? Is it the name of the submit input or the comment input?  Below is the form.

<form action="" method="post" >
	
         Enter your comment<br>
	<input type="text" name="created">
			
	<input type="submit" value="Post">
</form>
Link to comment
Share on other sites

Here is the full code. I still get the error that $createdday var is undefined.

                 $today = time();    
                 $createdday= strtotime($post['created']); //mysql timestamp of when post was created  
                 $datediff = abs($today - $createdday);  
                 $difftext="";  
                 $years = floor($datediff / (365*60*60*24));  
                 $months = floor(($datediff - $years * 365*60*60*24) / (30*60*60*24));  
                 $days = floor(($datediff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));  
                 $hours= floor($datediff/3600);  
                 $minutes= floor($datediff/60);  
                 $seconds= floor($datediff);  
                 //year checker  
                 if($difftext=="")  
                 {  
                   if($years>1)  
                    $difftext=$years." years ago";  
                   elseif($years==1)  
                    $difftext=$years." year ago";  
                 }  
                 //month checker  
                 if($difftext=="")  
                 {  
                    if($months>1)  
                    $difftext=$months." months ago";  
                    elseif($months==1)  
                    $difftext=$months." month ago";  
                 }  
                 //month checker  
                 if($difftext=="")  
                 {  
                    if($days>1)  
                    $difftext=$days." days ago";  
                    elseif($days==1)  
                    $difftext=$days." day ago";  
                 }  
                 //hour checker  
                 if($difftext=="")  
                 {  
                    if($hours>1)  
                    $difftext=$hours." hours ago";  
                    elseif($hours==1)  
                    $difftext=$hours." hour ago";  
                 }  
                 //minutes checker  
                 if($difftext=="")  
                 {  
                    if($minutes>1)  
                    $difftext=$minutes." minutes ago";  
                    elseif($minutes==1)  
                    $difftext=$minutes." minute ago";  
                 }  
                 //seconds checker  
                 if($difftext=="")  
                 {  
                    if($seconds>1)  
                    $difftext=$seconds." seconds ago";  
                    elseif($seconds==1)  
                    $difftext=$seconds." second ago";  
                 }  
                 echo " | ".$difftext;  

				$mydate = "$seconds | $minutes | $hours | $days | $months | $years";
				
				if(empty($post['created'])) {
				
					echo 'Please fill in the field';
					
				} else {
					
					comment	=	$_POST['created'];
					$insert  =  mysql_query("INSERT INTO posts (comment, posted)  VALUES($comment, $mydate)");
				}
				
				<form action="" method="post" >
					Enter your comment<br>
					<input type="text" name="created" >
					
				    <input type="submit" name="submit" value="upload">
			    </form>
Link to comment
Share on other sites

UPDATE

 

I had to change $post to $_POST.  It works now.

 

The only thing is the new date isn't shown in db. It's listed blank like this 0000-00-00 00:00:00. The 'posted' column which is set to timestamp. Do I need to modify it it in anyway to show the new type of formating?

Link to comment
Share on other sites

Have you tried using mysql_error() to see if any errors are being returned?

http://www.php.net/manual/en/function.mysql-error.php

 

Assuming that the "posted" column is using the datetime field type, you'll need to modify $mydate so that it matches the expected format.

 

 

I actually did change it to match the timestamp.

 

This is what normal timestamp looks like. 

date('Y-m-d H:i:s')

and this is mine.

$mydate = '$years | $months | $days | $hours | $minutes | $seconds';

Still no difference. The form is submiting correctly because I have other inputs that are being insert into the table as well.  It's just the $mydate is not being inserted correctly.

Link to comment
Share on other sites

No, you need to give MySQL a valid timestamp when adding a value to your (date) posted column.

$insert  =  mysql_query("INSERT INTO posts (comment, posted)  VALUES($comment, NOW())");

You'd only covert the timestamp to your facebook date format when getting it from the database.

Link to comment
Share on other sites

No, you need to give MySQL a valid timestamp when adding a value to your (date) posted column.

$insert  =  mysql_query("INSERT INTO posts (comment, posted)  VALUES($comment, NOW())");

You'd only covert the timestamp to your facebook date format when getting it from the database.

 

 

Oh I see. So I insert it as a normal timestamp as you put but when I query to get the date from the db, that's when I change it to facbook format? 

 

So say this is my get query.

$get	= 	mysql_query("SELECT * FROM posts");

while ($row = mysql_fetch_assoc($get)) {
    echo $row['posted'];  // this returns the date in this format ("Y-m-d H:i:s") 
}

How I convert it to facebook like date?

Link to comment
Share on other sites

You'd use $row['posted'] instead of $post['created'] on this line of code of the code in your OP

$createdday= strtotime($post['created']); //mysql timestamp of when post was created   

However, it'll be best to add that code you posted in your OP to a function, the just pass in $row['posted'] as an argument

 

The function definition

function facebook_date_format($timestamp)
{
    $today = time();    
    $createdday= strtotime($timestamp); //mysql timestamp of when post was created  
    $datediff = abs($today - $createdday);  
    $difftext="";  
    $years = floor($datediff / (365*60*60*24));  
    $months = floor(($datediff - $years * 365*60*60*24) / (30*60*60*24));  
    $days = floor(($datediff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));  
    $hours= floor($datediff/3600);  
    $minutes= floor($datediff/60);  
    $seconds= floor($datediff);  
    //year checker  
    if($difftext=="")  
    {  
     if($years>1)  
      $difftext=$years." years ago";  
     elseif($years==1)  
      $difftext=$years." year ago";  
    }  
    //month checker  
    if($difftext=="")  
    {  
      if($months>1)  
      $difftext=$months." months ago";  
      elseif($months==1)  
      $difftext=$months." month ago";  
    }  
    //month checker  
    if($difftext=="")  
    {  
      if($days>1)  
      $difftext=$days." days ago";  
      elseif($days==1)  
      $difftext=$days." day ago";  
    }  
    //hour checker  
    if($difftext=="")  
    {  
      if($hours>1)  
      $difftext=$hours." hours ago";  
      elseif($hours==1)  
      $difftext=$hours." hour ago";  
    }  
    //minutes checker  
    if($difftext=="")  
    {  
      if($minutes>1)  
      $difftext=$minutes." minutes ago";  
      elseif($minutes==1)  
      $difftext=$minutes." minute ago";  
    }  
    //seconds checker  
    if($difftext=="")  
    {  
      if($seconds>1)  
      $difftext=$seconds." seconds ago";  
      elseif($seconds==1)  
      $difftext=$seconds." second ago";  
    }  
    return $difftext;  
}

Now your while loop will be

while ($row = mysql_fetch_assoc($get)) {
    echo facebook_date_format($row['posted']); // pass the timestamp to the function, it'll return the date in facebook date format
} 

 
 

Link to comment
Share on other sites

You'd use $row['posted'] instead of $post['created'] on this line of code of the code in your OP

$createdday= strtotime($post['created']); //mysql timestamp of when post was created   

However, it'll be best to add that code you posted in your OP to a function, the just pass in $row['posted'] as an argument

 

The function definition

function facebook_date_format($timestamp)
{
    $today = time();    
    $createdday= strtotime($timestamp); //mysql timestamp of when post was created  
    $datediff = abs($today - $createdday);  
    $difftext="";  
    $years = floor($datediff / (365*60*60*24));  
    $months = floor(($datediff - $years * 365*60*60*24) / (30*60*60*24));  
    $days = floor(($datediff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));  
    $hours= floor($datediff/3600);  
    $minutes= floor($datediff/60);  
    $seconds= floor($datediff);  
    //year checker  
    if($difftext=="")  
    {  
     if($years>1)  
      $difftext=$years." years ago";  
     elseif($years==1)  
      $difftext=$years." year ago";  
    }  
    //month checker  
    if($difftext=="")  
    {  
      if($months>1)  
      $difftext=$months." months ago";  
      elseif($months==1)  
      $difftext=$months." month ago";  
    }  
    //month checker  
    if($difftext=="")  
    {  
      if($days>1)  
      $difftext=$days." days ago";  
      elseif($days==1)  
      $difftext=$days." day ago";  
    }  
    //hour checker  
    if($difftext=="")  
    {  
      if($hours>1)  
      $difftext=$hours." hours ago";  
      elseif($hours==1)  
      $difftext=$hours." hour ago";  
    }  
    //minutes checker  
    if($difftext=="")  
    {  
      if($minutes>1)  
      $difftext=$minutes." minutes ago";  
      elseif($minutes==1)  
      $difftext=$minutes." minute ago";  
    }  
    //seconds checker  
    if($difftext=="")  
    {  
      if($seconds>1)  
      $difftext=$seconds." seconds ago";  
      elseif($seconds==1)  
      $difftext=$seconds." second ago";  
    }  
    return $difftext;  
}

Now your while loop will be

while ($row = mysql_fetch_assoc($get)) {
    echo facebook_date_format($row['posted']); // pass the timestamp to the function, it'll return the date in facebook date format
} 

 

 

 

 

Simply brilliant!!! You have done it once again Ch0cu3r.  Thank you so much!!!! It works like charm.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.