Help - Search - Members - Calendar
Full Version: fuzzy time.
Movable Type Community Forum > Additional Resources > Tips and Tricks
brainchild2b
How can I have the generated date of the post show as "early morning", "mid morning", "lunch time", "afternoon", "evening", "past your bedtime" and so forth?

I use php on my site so a php script would work awesome or anything for that matter.
kadyellebee
Sure, how about something like this:

This must be in a MTEntries container, and doesn't have any formatting, just the message right now.
CODE
<?
//set the beginning time for each time section - keep the quotes in there
$earlymorn = strtotime("4:30am");
$midmorn = strtotime("9:00am");
$lunch = strtotime("11:30am");
$afternoon = strtotime("1:00pm");
$evening = strtotime("5:00pm");
$bedtime = strtotime("9:00pm");

$t = strtotime("<$MTEntryDate format="%X"$>");

//Modify the messages as necessary - note that the middle of the night one should be in there twice.
if ($t <= $earlymorn) { echo "Past Your Bedtime"; }
elseif ($t <= $midmorn) { echo "Early Morning"; }
elseif ($t <= $lunch) { echo "Mid-Morning"; }
elseif ($t <= $afternoon) { echo "Lunch time"; }
elseif ($t <= $evening) { echo "Afternoon"; }
elseif ($t <= $bedtime) { echo "Evening"; }
else echo "Past your bedtime";
?>


Post back if you have questions - it worked on my test blog smile.gif

Kristine
richarderiksson
Darn you Kristine, you just beat me.  

Here's my code:
CODE
<?php

$postedHour = <$MTEntryDate format="%k"$>;

// between 4 AM and 7 AM
if (($postedHour >= 4) && ($postedHour <= 7)) print "in the early morning";

// between 8 (inclusive) and noon
if (($postedHour >= 8) && ($postedHour < 12)) print "in the morning";

// self-explanatory
if ($postedHour == 12) print "during the noonhour";

// between 12 and 5
if (($postedHour > 12) && ($postedHour < 17)) print "in the afternoon";

// between 5 and 9 inclusive
if (($postedHour >= 17) && ($postedHour <= 21)) print "in the evening";

// after 10 PM and before 4 AM exclusive
if (($postedHour > 21) || ($postedHour < 4)) print "past your bedtime";

?>


Change the numbers as necessary.  

I like Kristine's code better.  Hmph!
kadyellebee
Oh, but thanks for posting yours, Richard - I didn't know how the syntax worked for when a value was between 2 numbers - so seeing your code was totally helpful to me!!  Its so hard to search for those things sometimes!

So mine is a bit flawed because it makes you enter in the words for the bedtime twice -- mainly because I didn't know the syntax you used!!! smile.gif

smile.gif
Kristine
brainchild2b
Okay i went throught the code and cleaned it up, it's fixed now, thanks for all your wonderful help!!!

one last problem:

scroll to the bottom of my weblog, see where the date changes from NOV 11 to NOV 10 from 10 to 1 the images I have for the numbers are showing nothing. what am I doing wrong? I tried 09.gif and 9.gif and neither shows. is there something i'm missing?
brainchild2b
nevermind i figured it out, since i didn't make an entry on the 9th duh. for single digit numbers you have to have a 09.gif when i tried that obviously nothing changed because the entry that i thought was the 9th was actually the 8th so i need 08.gif and the rest of them .

Thanks again for all the wonderful help!
kayseebay
i like this code...just one question...and don't snicker too much, im new...where do i PUT the code?

thanks
Ruth
kadyellebee
Wherever you want the date info to appear - usually in your Main Index template, somewhere inside the MTEntries container smile.gif

Hope that helps,
Kristine
maddy
I'm using Ms Kristine's code, and just replaced the bit in the "posted" portion of my main index and individual entry templates, and it works most excellently! I added the <MTEntryDate> tag to the title attribute for the permalink which wraps around it, as below! smile.gif

CODE
<div class="posted">
Posted by <$MTEntryAuthor$>, <a href="<$MTEntryPermalink$>" title="link to the post made at <MTEntryDate format="%X">">
<?
//set the beginning time for each time section - keep the quotes in there
$earlymorn = strtotime("3:30am");
$midmorn = strtotime("6:00am");
$lunch = strtotime("11:30am");
$afternoon = strtotime("1:00pm");
$evening = strtotime("5:00pm");
$bedtime = strtotime("11:00pm");

//This date format is for MT2.5 and above
$t = strtotime("<$MTEntryDate format="%X"$>");

//Modify the messages as necessary - note that the middle of the night one should be in there twice.
if ($t <= $earlymorn) { echo "while sleeping"; }
elseif ($t <= $midmorn) { echo "far too early"; }
elseif ($t <= $lunch) { echo "in the morning"; }
elseif ($t <= $afternoon) { echo "midday"; }
elseif ($t <= $evening) { echo "in the afternoon"; }
elseif ($t <= $bedtime) { echo "at night"; }
else echo "while sleeping";
?>
</a>, in <MTEntryCategories glue="& ">
<a href="<$MTCategoryArchiveLink$>" title="go to the category"><$MTCategoryLabel$></a>
</MTEntryCategories>t$>)</a></div>
mcroft
For those who aren't PHP enabled, but who could use something like Brad Choate's MT-Macros plugin, there's another way...

You'd define a bunch of macros thusly
CODE
<MTMacroDefine name="matins1" string="::00::">
Matins
</MTMacroDefine>
<MTMacroDefine name="matins2" string="::01::">
Matins
</MTMacroDefine>
<MTMacroDefine name="matins3" string="::02::">
Matins
</MTMacroDefine>
<MTMacroDefine name="Lauds1" string="::03::">
Lauds
</MTMacroDefine>
<MTMacroDefine name="Lauds2" string="::04::">
Lauds
</MTMacroDefine>
<MTMacroDefine name="Lauds3" string="::05::">
Lauds
</MTMacroDefine>
<MTMacroDefine name="Prime1" string="::06::">
Prime
</MTMacroDefine>

Then in your body, you'd use
CODE
<MTMacroApply>::<$MTEntryDate format="%H"$>::</MTMacroApply>


MT will make the 'date' ::03:: for this post, and the macro will change it to 'Matins'.

Notes: Obviously it needs to be extended through the rest of Prime, Terce, Sext, None, Vespers, and Compline.  Also someone with a better grasp of regular expressions than I have could easily make it so that the entries don't triple up...
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.