source: splees-fuzzy-datetime/trunk/splees_fuzzy_datetime.php @ 1580

Revision 1580, 16.6 KB checked in by Splee, 7 years ago (diff)

Small modifications that don't actually matter a jot in terms of operation. Cosmetic code changes only.

Line 
1<?php
2/*
3Plugin Name: Fuzzy DateTime
4Plugin URI: http://www.splee.co.uk/category/fuzzydate-plugin/
5Description: Prints a fuzzy date and time, similar to the KDE clock.
6Version: 0.6
7Author: Splee
8Author URI: http://www.splee.co.uk
9*/
10
11/*  Copyright 2004  Lee McFadden  (email : splee@splee.co.uk)
12
13    This program is free software; you can redistribute it and/or modify
14    it under the terms of the GNU General Public License as published by
15    the Free Software Foundation; either version 2 of the License, or
16    (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21    GNU General Public License for more details.
22
23    You should have received a copy of the GNU General Public License
24    along with this program; if not, write to the Free Software
25    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26*/
27
28/*
29This plugin contains code from the Time of Day plugin
30by Phu Ly & Dunstan Orchard & Michael Heilemann (http://www.ifelse.co.uk/code/timeofday.php)
31*/
32
33/*
34ChangeLog:
35
36               
370.6:    (02/05/05) - Updated code with fixes for months being detected 'too literally'
38                Fixed issue with day/days mode not being detected correctly
39                Code tidy and optimisation
40               
410.5:    Fixed issues with admin panel not saving options correctly
42
430.4:    Added admin panel functionality
44
450.3:    Fixed numerous issues with date calculations
46                Tidied code and added some meaningful comments
47
480.2:    Too much to list!
49
500.1:    First implimentation.
51*/
52//ini_set('display_errors', true); //for debugging
53
54// This is the function that needs to replace 'the_time()' in your theme.
55function splees_fuzzy_datetime() {
56        //Get the options from the db
57        $lm_fdt_options = get_option('lm_fdt_options');
58        $lm_time_offset = get_settings('gmt_offset') * 3600;
59       
60        //get the two timestamps
61        $nowStamp = strtotime(gmdate('Y-m-d H:i:s', time()));
62        $postStamp = strtotime(get_post_time('Y-m-d H:i:s', true));
63        $difference = $lm_nowStamp - $lm_postStamp;
64       
65       
66        //Spot the difference...
67        $dayDifference = (date('z', $nowStamp) - date('z', $postStamp));
68        $monthDifference = (date('n', $nowStamp) - date('n', $postStamp));
69        $yearDifference = (date('Y', $nowStamp) - date('Y', $postStamp));
70       
71        if ($yearDifference > 1) $fuzzyMode = "years";
72        elseif ($yearDifference == 1) $fuzzyMode = "year";
73        elseif ($monthDifference > 1) $fuzzyMode = "months";
74        elseif (($monthDifference == 1) && ($dayDifference >= 14)) $fuzzyMode = "month";  //Added a two week bias before the post was "last month".
75        elseif ($dayDifference >= 14) $fuzzyMode = "weeks";
76        elseif ($dayDifference >= 7) $fuzzyMode = "week";
77        elseif ($dayDifference > 1) $fuzzyMode = "days";
78        elseif ($dayDifference == 1) $fuzzyMode = "yesterday";
79        elseif ($dayDifference == 0) $fuzzyMode = "today";
80
81        //these are the strings to be replaced
82        $rewritecode = array(
83                "%minutesSince%",
84                "%hoursSince%",
85                "%daysSince%",
86                "%weeksSince%",
87                "%monthsSince%",
88                "%yearsSince%",
89                "%dayName%",
90                "%monthName%",
91                "%weekOfMonth%",
92                "%fuzzyTimeText%",
93                "%fuzzy12Hour%",
94                "%fuzzyWeekPeriod%",
95                "%fuzzyMonthPeriod%",
96                "%MinutesOrHoursSince%",
97                "%actualDate%",
98                "%actualTime%"
99                );
100
101        //get the replacements for the above
102        $rewritereplace = lm_get_fuzzy_replacements($postStamp, $nowStamp, $lm_time_offset);
103
104        $outString = str_replace($rewritecode, $rewritereplace, $lm_fdt_options[$fuzzyMode]);
105       
106        //now print the fuzzy datetime.
107        print $outString;
108}
109
110
111/* lm_get_fuzzy_replacements:
112        This function must return an array the same length as the
113        $rewriteCode array in splees_fuzzy_datetime()
114       
115        Parameters
116        ----------
117        $postStamp: This should be the timestamp of the post
118        $nowStamp: This should be the timestamp for now()
119*/
120function lm_get_fuzzy_replacements($postStamp, $nowStamp, $lm_time_offset) {
121        $difference = ($nowStamp - $postStamp);
122        //Now the delta has been calculated, go with the blog's local time
123        $postStamp = $postStamp + $lm_time_offset;
124        $nowStamp = $nowStamp + $lm_time_offset;
125       
126        /*      Must return an array with the results in the following order:
127       
128                "%minutesSince%",
129                "%hoursSince%",
130                "%daysSince%",
131                "%weeksSince%",
132                "%monthsSince%",
133                "%yearsSince%",
134                "%dayName%",
135                "%monthName%",
136                "%weekOfMonth%",
137                "%fuzzyTimeText%",
138                "%fuzzy12Hour%",
139                "%fuzzyWeekPeriod%",
140                "%fuzzyMonthPeriod%",
141                "%MinutesOrHoursSince%",
142                "%actualDate%",
143                "%actualTime%"
144        */
145        $returnValue = array();
146        // %minutesSince%
147        $returnValue[0] = floor($difference/60);
148        //%hoursSince%
149        $returnValue[1] = floor(($difference/60)/60);
150        //%daysSince%
151        $returnValue[2] = (date('z', $nowStamp) - date('z', $postStamp)); //floor((($difference/60)/60)/24);
152        //%weeksSince%
153        $returnValue[3] = floor(((($difference/60)/60)/24)/7);
154        //%monthsSince%
155        $returnValue[4] = (date('n', $nowStamp) - date('n', $postStamp));
156        //%yearsSince%
157        $returnValue[5] = (date('Y', $nowStamp) - date('Y', $postStamp));
158               
159                if ($returnValue[5] > 0) {
160                        //deal with the inverted %monthsSince% value if %yearsSince% > 0
161                        $returnValue[4] = $returnValue[4] * (-1);
162                        //Also, the daysSince tag will start to go skewiff when calculated in a different year.
163                        //fix this by being semantically less accurate but numerically more accurate.
164                        $returnValue[2] = floor(((($difference/60)/60)/24)/7);
165                }
166        //%dayName%
167        $returnValue[6] = date('l', $postStamp);
168        //%monthName%
169        $returnValue[7] = date('F', $postStamp);
170        //%weekOfMonth%
171        $returnValue[8] = ceil(date('j', $postStamp)/7);
172        switch ($returnValue[8]) {
173                case 1:
174                        $returnValue[8] .= "st";
175                        break;
176                case 2:
177                        $returnValue[8] .= "nd";
178                        break;
179                case 3:
180                        $returnValue[8] .= "rd";
181                        break;
182                default:
183                        $returnValue[8] .= "th";
184                        break;
185        }
186        //%fuzzyTimeText%
187        $returnValue[9] = lm_get_fuzzyTimeText($postStamp);
188        //%fuzzy12Hour%
189        $returnValue[10] = lm_get_fuzzy12Hour($postStamp);
190        // %fuzzyWeekPeriod%
191        switch(date('w', $postStamp)) {
192                case 0:
193                        //silly PHP people think sunday is the beginning of the week! *rolls eyes*
194                        $returnValue[11] = "at the weekend";
195                        break;
196                case 1:
197                case 2:
198                        $returnValue[11] = "at the beginning of the week";
199                        break;
200                case 3:
201                case 4:
202                        $returnValue[11] = "midweek";
203                        break;
204                case 5:
205                case 6:
206                        $returnValue[11] = "at the weekend";
207                        break;
208                default:
209                        //we don't know what day it was posted, so we'll be super vague here.
210                        //it was this or an error message; at least this kinda fits in ;)
211                        $returnValue[11] = "at some point during a 7 day period";
212                        break;
213        }
214        // %fuzzyMonthPeriod%
215        // Use the previously calculated monthname
216        switch(ceil(date('j', $postStamp)/7)) {
217                case 1:
218                        $returnValue[12] = "at the start of " . $returnValue[7];
219                        break;
220                case 2:
221                case 3:
222                        $returnValue[12] = "mid-" . $returnValue[7];
223                        break;
224                case 4:
225                case 5:
226                        $returnValue[12] = "at the end of " . $returnValue[7];
227                        break;
228                default:
229                        //again with the nice camouflaged error messages.
230                        $returnValue[12] = "at some point in " . $returnValue[7];
231                        break;
232        }
233       
234        //%fuzzyMinutesOrHours%
235        //use the previously calculated %hoursSince% and %minutesSince%
236        if ($returnValue[1] != 0) {
237                //if it's been over an hour:
238                if ($returnValue[1] > 1) {
239                        $returnValue[13] = $returnValue[1] . " hours";
240                } else {
241                        $returnValue[13] = "1 hour";
242                }
243        } else {
244                //it's been less than an hour
245                if ($returnValue[0] > 1) {
246                        //it's more than 1 minute ago
247                        $returnValue[13] = $returnValue[0] . " minutes";
248                } elseif ($returnValue[0] = 1) {
249                        $returnValue[13] = "1 minute";
250                } else {
251                        //the chances are small, but it's been posted less than a minute ago
252                        $returnValue[13] = "less than a minute";
253                }
254        }
255        //%actualDate%
256        $returnValue[14] = date('D, d M Y', $postStamp);
257       
258        //%actualTime%
259        $returnValue[15] = date('g:i a', $postStamp);
260       
261        return $returnValue;
262}
263
264/*
265        lm_get_fuzzy12Hour:
266       
267        This function returns a string containing a
268        fuzzy representation of the 12 hour clock.
269       
270        Parameters
271        ----------
272        $postStamp: This should be the timestamp of the post
273*/
274function lm_get_fuzzy12Hour($postStamp) {
275        //fuzzify the hour
276        $fuzHours = array(
277                "one",
278                "two",
279                "three",
280                "four",
281                "five",
282                "six",
283                "seven",
284                "eight",
285                "nine",
286                "ten",
287                "eleven",
288                "twelve"
289                );
290        $returnVal = $fuzHours[(date('g', $postStamp)-1)];
291        //fuzzify the minutes
292        $postMins = date('i', $postStamp);
293        if (($postMins >= 7) && ($postMins <= 26)) {
294                //It's around quarter past
295                $returnVal = "a quarter past " . $returnVal;
296        } elseif (($postMins >= 27) && ($postMins <= 37)) {
297                //it's around half past
298                $returnVal = "half-past " . $returnVal;
299        } elseif (($postMins >= 38) && ($postMins <= 47)) {
300                $returnVal = "a quarter to " . $returnVal;
301        }
302        $returnVal .=  " " . date('a', $postStamp);
303        return $returnVal;
304}
305
306/*
307        lm_get_fuzzyTimeText:
308        Returns a string with a fuzzy representation of the time
309        period from the $postStamp param.
310       
311        *based on code from the timeofday plugin by
312        Phu Ly & Dunstan Orchard & Michael Heilemann
313        (http://www.ifelse.co.uk/code/timeofday.php)*
314       
315        Parameters
316        ----------
317        $postStamp: This should be the timestamp of the post
318*/
319function lm_get_fuzzyTimeText($postStamp) {
320        switch(date('G', $postStamp))
321                {
322                case 0:
323                case 1:
324                case 2:
325                        $returnVal = 'in the wee hours';
326                        break;
327                case 3:
328                case 4:
329                case 5:
330                case 6:
331                        $returnVal = 'terribly early in the morning';
332                        break;
333                case 7:
334                case 8:
335                case 9:
336                        $returnVal = 'in the early morning';
337                        break;
338                case 10:
339                        $returnVal = 'mid-morning';
340                        break;
341                case 11:
342                        $returnVal = 'just before lunchtime';
343                        break;
344                case 12:
345                case 13:
346                        $returnVal = 'around lunchtime';
347                        break;
348                case 14:
349                        $returnVal = 'in the early afternoon';
350                        break;
351                case 15:
352                case 16:
353                        $returnVal = 'mid-afternoon';
354                        break;
355                case 17:
356                        $returnVal = 'in the late afternoon';
357                        break;
358                case 18:
359                case 19:
360                        $returnVal = 'in the early evening';
361                        break;
362                case 20:
363                case 21:
364                        $returnVal = 'at around evening time';
365                        break;
366                case 22:
367                        $returnVal = 'in the late evening';
368                        break;
369                case 23:
370                        $returnVal = 'late at night';
371                        break;
372                default:
373                        $returnVal = '';
374                        break;
375                }
376        return $returnVal;
377}
378
379/*
380        From here on out we are dealing with the admin GUI.
381*/
382
383function lm_fdt_options_page() {
384        switch($_POST['action']) {
385                case 'lm_fdt_update':
386                        //The options have just been updated...  so update em already!
387                        $lm_fdt_options["today"] = $_POST['today'];
388                        $lm_fdt_options["yesterday"] = $_POST['yesterday'];
389                        $lm_fdt_options["days"] = $_POST['days'];
390                        $lm_fdt_options["week"] = $_POST['week'];
391                        $lm_fdt_options["weeks"] = $_POST['weeks'];
392                        $lm_fdt_options["month"] = $_POST['month'];
393                        $lm_fdt_options["months"] = $_POST['months'];
394                        $lm_fdt_options["year"] = $_POST['year'];
395                        $lm_fdt_options["years"] = $_POST['years'];
396                       
397                        update_option('lm_fdt_options', $lm_fdt_options);
398                        break;
399                default:
400                        //Add the defaults to the db if they're not there already.
401                        $lm_fdt_defaults["years"] = "%yearsSince% years ago, %fuzzyMonthPeriod%";
402                        $lm_fdt_defaults["year"] = "last year, %fuzzyMonthPeriod%";
403                        $lm_fdt_defaults["months"] = "%monthsSince% months ago";
404                        $lm_fdt_defaults["month"] = "1 month ago";
405                        $lm_fdt_defaults["weeks"] = "%dayName%, %weeksSince% weeks ago";
406                        $lm_fdt_defaults["week"] = "%dayName%, last week";
407                        $lm_fdt_defaults["days"] = "%daysSince% days ago, %fuzzyTimeText%";
408                        $lm_fdt_defaults["yesterday"] = "yesterday, %fuzzyTimeText%";
409                        $lm_fdt_defaults["today"] = "%MinutesOrHoursSince% ago";
410                        add_option('lm_fdt_options', $lm_fdt_defaults);
411                        break;
412        }
413        $lm_fdt_options = get_option('lm_fdt_options');
414
415//the page itself
416?>
417<div class="wrap">
418<form name="lm_fdt_options" method="post" action="<?php echo $_SERVER[PHP_SELF]; ?>?page=splees_fuzzy_datetime.php">
419<input type="hidden" name="action" value="lm_fdt_update" />
420        <h2>Output Strings</h2>
421        <p>Each of these options configure the output depending on the amount of time passed.  Enter the string using the replacement
422        variables which are explained at the bottom of the page.</p>
423        <table width="100%" cellpadding="5">
424                <tr>
425                        <th valign="top" scope="row" align="right">Today:</th>
426                        <td>
427                                <INPUT type="text" name="today" value="<?php echo $lm_fdt_options["today"]; ?>" size="70" />
428                        </td>
429                </tr>
430                <tr>
431                        <th valign="top" scope="row" align="right">Yesterday:</th>
432                        <td>
433                                <INPUT type="text" name="yesterday" value="<?php echo $lm_fdt_options["yesterday"]; ?>" size="70" />
434                        </td>
435                </tr>
436                <tr>
437                        <th valign="top" scope="row" align="right">More than 2 days ago:</th>
438                        <td>
439                                <input type="text" name="days" value="<?php echo $lm_fdt_options["days"] ?>" size="70" />
440                        </td>
441                </tr>
442                <tr>
443                        <th valign="top" scope="row" align="right">Last week:</th>
444                        <td>
445                                <input type="text" name="week" value="<?php echo $lm_fdt_options["week"] ?>" size="70" />
446                        </td>
447                </tr>
448                <tr>
449                        <th valign="top" scope="row" align="right">More than 1 week ago:</th>
450                        <td>
451                                <input type="text" name="weeks" value="<?php echo $lm_fdt_options["weeks"] ?>" size="70" />
452                        </td>
453                </tr>
454                <tr>
455                        <th valign="top" scope="row" align="right">Last month:</th>
456                        <td>
457                                <input type="text" name="month" value="<?php echo $lm_fdt_options["month"] ?>" size="70" />
458                        </td>
459                </tr>
460                <tr>
461                        <th valign="top" scope="row" align="right">More than 1 month ago:</th>
462                        <td>
463                                <input type="text" name="months" value="<?php echo $lm_fdt_options["months"] ?>" size="70" />
464                        </td>
465                </tr>
466                <tr>
467                        <th valign="top" scope="row" align="right">Last year:</th>
468                        <td>
469                                <input type="text" name="year" value="<?php echo $lm_fdt_options["year"] ?>" size="70" />
470                        </td>
471                </tr>
472                <tr>
473                        <th valign="top" scope="row" align="right">More than a year ago:</th>
474                        <td>
475                                <input type="text" name="years" value="<?php echo $lm_fdt_options["years"] ?>" size="70" />
476                        </td>
477                </tr>
478
479        </table>
480        <p class="submit"><input type="submit" value="Update Options"></p>
481       
482        <fieldset class="options">
483        <legend>Available replacements</legend>
484        <table width="100%" cellspacing="2" cellpadding="5">
485        <tr>
486        <th>String</th><th>Description</th><th>Example usage</th>
487        </tr>
488        <tr>
489        <td>%minutesSince%</td><td>Displays the minutes since the post</td><td>Posted %minutesSince% minutes ago</td>
490        </tr>
491        <tr>
492        <td>%hoursSince%</td><td>Displays the hours since the post</td><td>Posted %hoursSince% hours ago</td>
493        </tr>
494        <tr>
495        <td>%daysSince%</td><td>Displays the days since the post</td><td>Posted %daysSince% days ago</td>
496        </tr>
497        <tr>
498        <td>%weeksSince%</td><td>Displays the weeks since the post</td><td>Posted %weeksSince% weeks ago</td>
499        </tr>
500        <tr>
501        <td>%monthsSince%</td><td>Displays the months since the post</td><td>Posted %monthsSince% months ago</td>
502        </tr>
503        <tr>
504        <td>%yearsSince%</td><td>Displays the years since the post</td><td>Posted %yearsSince% years ago</td>
505        </tr>
506        <tr>
507        <td>%dayName%</td><td>Displays the name of the day when the post was published</td><td>Posted on a %dayName%</td>
508        </tr>
509        <tr>
510        <td>%monthName%</td><td>Displays the name of the month when the post was published</td><td>Posted during %monthName%</td>
511        </tr>
512        <tr>
513        <td>%weekOfMonth%</td><td>Displays the week of the month in which the post was published</td><td>Posted during the %weekOfMonth% week of %monthName%</td>
514        </tr>
515        <tr>
516        <td>%fuzzyTimeText%</td><td>Displays the time, fuzzily</td><td>Posted yesterday, %fuzzyTimeText%</td>
517        </tr>
518        <tr>
519        <td>%fuzzy12Hour%</td><td>Displays the time, fuzzily, in a 12 hour format</td><td>Posted today %fuzzy12Hour%</td>
520        </tr>
521        <tr>
522        <td>%fuzzyWeekPeriod%<td>Displays the general time of the week the post was published</td><td>Posted last week, %fuzzyWeekPeriod%</td>
523        </tr>
524        <tr>
525        <td>%fuzzyMonthPeriod%</td><td>Displays the general time of the month the post was published</td><td>Posted last year, %fuzzyMonthPeriod%</td>
526        </tr>
527        <tr>
528        <td>%MinutesOrHoursSince%</td><td>Diplays the minutes or hours since the post was published, usually used when the post was published that day.</td><td>Posted %MinutesOrHoursSince% ago</td>
529        </tr>
530        <tr>
531        <td>%actualDate%</td><td>Displays the date the post was published</td><td>Posted %actualDate%</td>
532        </tr>
533        <tr>
534        <td>%actualTime%</td><td>Displays the time the post was published</td><td>Posted %actualTime%</td>
535        </tr>
536        </table>
537        </fieldset>
538</form>
539
540</div>
541<?php
542}
543
544/* Add the above pages to the wp-admin GUI */
545function lm_fdt_add_pages() {
546        add_options_page('Fuzzy DateTime', 'Fuzzy DateTime', 8, basename(__FILE__), 'lm_fdt_options_page');
547}
548add_action('admin_menu', 'lm_fdt_add_pages');
549?>
Note: See TracBrowser for help on using the repository browser.