View Full Version : Good at Maths?
Jamie
January 23rd, 2005, 14:09
This is probably going to take somebody good at maths, fancy your chances?
In this bespoke blog that dirmass and a few others have been helping me with I am wanting to display how long ago the post was entered rather than the date it was entered, so here is what I am thinking...
Insert a new text field in to the submit_entry.php form. This text field would have a value of
[syntax:e31f3b774a="php"]
<?php
$posted_on = date("YmdH:m");
echo $posted_on;
?>
[/syntax:e31f3b774a]
On submit it would send this to the script, the script would then do exactly the same thing, eg. find todays date and time using date("YmdH:m");
Then somehow, probably using a combination of subtract and divide it would calculate the time in the format of Posted X Days, X Hours Ago.
To me it sounds like a pretty complicated script, at least it does once you get in to months and years old.
Anyone care to take up the challenge?
sonicgroup
January 23rd, 2005, 14:38
I take no credit for this. This was posted in the comments in the PHP Manual by a user. It's a function that works similar to Microsoft's datediff() in Visual Basic and such.
[syntax:6a62647711="php"]
//function like dateDiff Microsoft
//bug update for previous
function dateDiff($interval,$dateTimeBegin,$dateTimeEnd) {
//Parse about any English textual datetime
//$dateTimeBegin, $dateTimeEnd
$dateTimeBegin=strtotime($dateTimeBegin);
if($dateTimeBegin === -1) {
return("..begin date Invalid");
}
$dateTimeEnd=strtotime($dateTimeEnd);
if($dateTimeEnd === -1) {
return("..end date Invalid");
}
$dif=$dateTimeEnd - $dateTimeBegin;
switch($interval) {
case "s"://seconds
return($dif);
case "n"://minutes
return(floor($dif/60)); //60s=1m
case "h"://hours
return(floor($dif/3600)); //3600s=1h
case "d"://days
return(floor($dif/86400)); //86400s=1d
case "ww"://Week
return(floor($dif/604800)); //604800s=1week=1semana
case "m": //similar result "m" dateDiff Microsoft
$monthBegin=(date("Y",$dateTimeBegin)*12)+
date("n",$dateTimeBegin);
$monthEnd=(date("Y",$dateTimeEnd)*12)+
date("n",$dateTimeEnd);
$monthDiff=$monthEnd-$monthBegin;
return($monthDiff);
case "yyyy": //similar result "yyyy" dateDiff Microsoft
return(date("Y",$dateTimeEnd) - date("Y",$dateTimeBegin));
default:
return(floor($dif/86400)); //86400s=1d
}
}
[/syntax:6a62647711]
Jamie
January 23rd, 2005, 14:41
A lot larger of a script than I anticipated. Thanks Dave, i'll give it a go.
Jamie
January 23rd, 2005, 14:51
Can't seem to get it working, I won't bore you by asking you have a look though because to me it looks like one hell of a handful that isn't worth the bother, I'll stick to the standard date. ;) Thanks anyway Dave.
Alex
January 23rd, 2005, 15:27
After playing with that function I came up with this:
[syntax:a2923940f6="php"]
<?php
function dateDiff($interval,$dateTimeBegin,$dateTimeEnd) {
$dateTimeBegin=strtotime($dateTimeBegin);
if($dateTimeBegin === -1) {
return("..begin date Invalid");
}
$dateTimeEnd=strtotime($dateTimeEnd);
if($dateTimeEnd === -1) {
return("..end date Invalid");
}
$dif=$dateTimeEnd - $dateTimeBegin;
switch($interval) {
case "s"://seconds
return($dif);
case "n"://minutes
return(floor($dif/60)); //60s=1m
case "h"://hours
return(floor($dif/3600)); //3600s=1h
case "d"://days
return(floor($dif/86400)); //86400s=1d
case "ww"://Week
return(floor($dif/604800)); //604800s=1week=1semana
case "m": //similar result "m" dateDiff Microsoft
$monthBegin=(date("Y",$dateTimeBegin)*12)+
date("n",$dateTimeBegin);
$monthEnd=(date("Y",$dateTimeEnd)*12)+
date("n",$dateTimeEnd);
$monthDiff=$monthEnd-$monthBegin;
return($monthDiff);
case "yyyy": //similar result "yyyy" dateDiff Microsoft
return(date("Y",$dateTimeEnd) - date("Y",$dateTimeBegin));
default:
return(floor($dif/86400)); //86400s=1d
}
}
$date = date("j F Y");
if($_POST["date"] || $_POST["interval"]){
$userdate = $_POST["date"];
$interval = $_POST["interval"];
echo "Todays date: ($date) minus the date you entered ($userdate) makes " . dateDiff("$interval","$userdate","$date");
}
?>
<form action="<?php $_PHP["SELF"]; ?>" method="post">
Begin date: <input type="text" name="date" /><br />
Interval: <input type="text" name="interval" />
<input type="submit" value="get difference" />
</form>
Example:<br />
Begin date <b>20 january 2005</b><br />
Interval : <b>d</b> (in days)
[/syntax:a2923940f6]
Jamie
January 23rd, 2005, 15:36
Thanks Alex. I'll take a look at it now.
Anoop
January 24th, 2005, 15:37
You can add a field in database table to store the time stamp corresponding to the modified time. The function time() will give the current time stamp. When ever the content is updated, you can update the database field.
Then to calculate how long ago the post was entered/ modified, get the time stamp using the same function( ie time()). Get the difference between the current time and the time stamp in the database, which will give the time difference in seconds. Then as we know the relation ween second, minute and hour , we can simply format the data as we like. Simple!! :)
sonicgroup
January 24th, 2005, 16:53
That would be my recommendation as well. Add an integer field to your table and store dates as timestamps ( UNIX_TIMESTAMP() function in MySQL). Then you can use the time() and date() functions from PHP on that timestamp to generate a human readable date.
The nice thing about using timestamps, as Anoop mentioned, is that they are represented in seconds since the Unix epoch - so it's all one big integer, and easy to do math on.
vBulletin® v3.6.8, Copyright ©2000-2010, Jelsoft Enterprises Ltd.