PDA

View Full Version : date (n days ago) hack


mustbme
05-01-2006, 03:16 PM
UPDATE: Don't do it! It's pretty, but his modification breaks the p2_news plugin.

This hack may break other plugins, or SD itself.

Do not add this modification to SD, instead, request something similar be added in a future release of SD!

See initial post "I'm not suggesting anyone modify their installation."

ORIGINAL POST:
I'm not a programmer, so I may have taken the wrong approach here, and I'm not suggesting anyone modify their installation, this just happened to work nicely on a test site. Hopefully I didn't reinvent the wheel... Tested *only* with news and latest news plugins - I'm sure it will adversely affect something else, somewhere, but it would be cool to see something like this included down the road... (hint)

Code as attachment below.

Add to table sd_mainsettings:

settingid: [leave blank (auto inc)]
varname: datehack
groupname: Date and Time Options
input: yesno
title: Date Hack (Human Readable)
description: Appends (today, yesterday, n days ago) to DisplayDate. [ OR Similar Text ]
value: 1

settingid: [leave blank (auto inc)]
varname: datehacklimit
groupname: Date and Time Options
input: text
title: Date Hack Limit
description: Limit number of Days (ex: 365) to display datehack. Empty or 0 defaults to forever. [ OR Similar Text ]
value: 365

Add to globalfunctions.php, immediately before DisplayDate()

function humanreadable($date = NULL) {
global $mainsettings;
$days = (mktime() - $date) / 86400;
if(&#036;days < 1) {
&#036;result = &#39;Today&#39;;
} elseif(&#036;days < 2) {
&#036;result = &#39;Yesterday&#39;;
} else {
&#036;result = ceil(&#036;days) .&#39; Days Ago&#39;;
}
if ((&#036;days < (&#036;mainsettings[&#39;datehacklimit&#39;] + 1)) || (&#33;&#036;mainsettings[&#39;datehacklimit&#39;])){
return &#39; (&#39; . &#036;result . &#39;)&#39;;
} else
return &#39;&#39;;
}

Modify DisplayDate()

Change bottom line in function from:

return &#036;gmepoch ? strtr(@gmdate(&#036;dateformat, &#036;gmepoch + (3600 * &#036;timezoneoffset + &#036;dst)),

To:

if (&#036;mainsettings[&#39;datehack&#39;]) {
return &#036;gmepoch ? strtr(@gmdate(&#036;dateformat, &#036;gmepoch + (3600 * &#036;timezoneoffset + &#036;dst)), &#036;sdlanguage) . &#39; &#39; . humanreadable(&#036;gmepoch + (3600 * &#036;timezoneoffset + &#036;dst)) : &#39;&#39;;
} else {
return &#036;gmepoch ? strtr(@gmdate(&#036;dateformat, &#036;gmepoch + (3600 * &#036;timezoneoffset + &#036;dst)), &#036;sdlanguage) : &#39;&#39;;
}

Results:
<div align="center">http://www.mustbme.com/images/datehack1.jpg</div>

<div align="center">http://www.mustbme.com/images/datehack2.jpg</div>

<div align="center">http://www.mustbme.com/images/datehack3.jpg</div>


[attachmentid=340]

72dpi
05-01-2006, 03:36 PM
I&#39;m a fan of showing this kind of date format.
nice hack =)

Mighty_Mouse_2006
05-01-2006, 05:05 PM
Very Nice, Thank you.

abcohen
05-01-2006, 05:23 PM
claps well done... love the addition of extra setting (maybe a staffer will add this to the next version [but we&#39;ll need a language setting for yesturday and days])

Robert_J_Ellis
05-01-2006, 06:01 PM
Great work, something like this is perfect for many sites.

thomas
05-02-2006, 12:28 AM
awsome, that would also be good for the event manager

Mighty_Mouse_2006
05-02-2006, 12:53 AM
<div class='quotetop'>QUOTE(thomas &#064; May 1 2006, 07&#58;28 PM) 5524</div>
awsome, that would also be good for the event manager
[/b]

It will be there. It will be any place the date is shown using SD&#39;d DisplatDate() function.

thomas
05-02-2006, 02:03 AM
dose this also work in the other direction with a few changes of the code?

3 days to go
Today

Just wondering?

mustbme
05-02-2006, 06:05 AM
UPDATE: Don&#39;t do it&#33; It&#39;s pretty, but his modification breaks the p2_news plugin.

This hack may break other plugins, or SD itself.

Do not add this modification to SD, instead, request something similar be added in a future release of SD&#33;

See initial post "I&#39;m not suggesting anyone modify their installation."

ORIGINAL POST:
Enhanced date display function. Goes both ways (n days ago, n days to go), nicer output.

Ignore "datehacklimit" previously mentioned. You can also ignore datehack, and not modify a mySQL table previously mentioned if you change the original line in DisplayDate() from this:

**return &#036;gmepoch ?**strtr&#40;@gmdate&#40;&#036;dateformat, &#036;gmepoch + &#40;3600 * &#036;timezoneoffset + &#036;dst&#41;&#41;, &#036;sdlanguage&#41; &#58; &#39;&#39;;


To this:

** return &#036;gmepoch ?**strtr&#40;@gmdate&#40;&#036;dateformat, &#036;gmepoch + &#40;3600 * &#036;timezoneoffset + &#036;dst&#41;&#41;, &#036;sdlanguage&#41; . &#39; &#39; . humanreadable&#40;&#036;gmepoch + &#40;3600 * &#036;timezoneoffset + &#036;dst&#41;&#41; &#58; &#39;&#39;;


Add the following code (Also appears in attached text file: [attachmentid=342]) in place of prior humanreadable() function appearing in a previous post.

I did not write this function, it appears on a public domain CD image I downloaded. I added test for negative result, called abs() function to remove the negative sign, so it would workwith future dates also, &#39;To Go&#39; and &#39;Ago&#39;, and parentheses around returned string. This relies on the standard UNIX timestamp returned from an OS, which is what SD uses to represent times and dates. There are a million ways to do what this function does, it was the coolest of all those I happened to stumble upon.

function humanreadable&#40;&#036;original&#41; {
****
****&#036;chunks = array&#40;**// array of time period chunks
********array&#40;60 * 60 * 24 * 365 , &#39;Year&#39;&#41;,
********array&#40;60 * 60 * 24 * 30 , &#39;Month&#39;&#41;,
********array&#40;60 * 60 * 24 * 7, &#39;Week&#39;&#41;,
********array&#40;60 * 60 * 24 , &#39;Day&#39;&#41;,
********array&#40;60 * 60 , &#39;Hour&#39;&#41;,
********array&#40;60 , &#39;Minute&#39;&#41;,
****&#41;;
****
****&#036;today = mktime&#40;&#41;; /* Current unix time***/
****&#036;since = &#036;today - &#036;original;

****if &#40;&#036;since &#60; 0&#41; { &#036;since = abs&#40;&#036;since&#41;; &#036;pfix = &#39; To Go&#39;; } else { &#036;pfix = &#39; Ago&#39;; }
****
****// &#036;jcount saves performing the count function each time around the loop
****for &#40;&#036;icount = 0, &#036;jcount = count&#40;&#036;chunks&#41;; &#036;icount &#60; &#036;jcount; &#036;icount++&#41; {
********
********&#036;seconds = &#036;chunks&#91;&#036;icount&#93;&#91;0&#93;;** &#036;name = &#036;chunks&#91;&#036;icount&#93;&#91;1&#93;;
********
********// finding the biggest chunk &#40;if the chunk fits, break&#41;
********if &#40;&#40;&#036;count = floor&#40;&#036;since / &#036;seconds&#41;&#41; &#33;= 0&#41; {
************break;
********}
****}
****
****&#036;print = &#40;&#036;count == 1&#41; ? &#39;1 &#39;.&#036;name &#58; &#34;&#036;count {&#036;name}s&#34;;
****
****if &#40;&#036;icount + 1 &#60; &#036;jcount&#41; {
********// now getting the second item
********&#036;seconds2 = &#036;chunks&#91;&#036;icount + 1&#93;&#91;0&#93;;** &#036;name2 = &#036;chunks&#91;&#036;icount + 1&#93;&#91;1&#93;;
********
********// add second item if it&#39;s greater than 0
********if &#40;&#40;&#036;count2 = floor&#40;&#40;&#036;since - &#40;&#036;seconds * &#036;count&#41;&#41; / &#036;seconds2&#41;&#41; &#33;= 0&#41; {
************&#036;print .= &#40;&#036;count2 == 1&#41; ? &#39;, 1 &#39;.&#036;name2 &#58; &#34;, &#036;count2 {&#036;name2}s&#34;;
********}
****}
****return &#39;&#40;&#39; .&#036;print . &#036;pfix . &#39;&#41;&#39;;
}

If any of this is confusing, please see original post. This function drops in, in place of and replacing the previous humanreadable() function show above, providing better display results. Again, I am not suggesting anyone modify their standard SD installation.

Sample:

http://www.mustbme.com/images/humanreadable.jpg

.

bopheim
05-02-2006, 10:40 AM
This is awsome. Please include this option in new release..&#33;

mustbme
05-03-2006, 04:51 PM
Don&#39;t do it&#33; It&#39;s pretty, but his modification breaks the p2_news plugin.

This hack may break other plugins, or SD itself.

Do not add this modification to SD, instead, request something similar be added in a future release of SD&#33;

See initial post "I&#39;m not suggesting anyone modify their installation."

hello?

Loverboy27
10-29-2006, 03:13 AM
added, works perfectly...

more than just thx ;)

thomas
05-07-2007, 06:20 AM
can we look at including this in the next SD release?