/    Sign up×
Community /Pin to ProfileBookmark

Echo Text & Date

This is something I have used, but I need to enhance it a bit.

OLD
If the day = DATE SPECIFIED, then echo LINE ASSOCIATED WITH DATE SPECIFIED

[code=php]$today = date(“md”);// that’s month day, both 2 digits.
if($today < “0115”)
//if($today < “0200”)// has to be first month like this.
echo ” – Happy New Year”;
else if($today == “0214”)// second month, 14th day.
echo ” – Happy Valentines Day”;
else if($today == “0704”)// seventh month, 4th day.
echo ” – Happy 4th of July”;
else if($today >= “1115” && $today <= “12-31″) //11-15…12-31 Holiday Greetings
echo ” – Happy Holidays”;
else
echo “”; [/code]

How can I do this?
DATE SPECIFIED – LINE – Happy New Year
DATE SPECIFIED – LINE – Happy Valentines Day
DATE SPECIFIED – LINE – Happy 4th of July

Echo Line if date == DATE SPECIFIED

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@SyCoSep 17.2008 — You want to do this

How can I do this?

DATE SPECIFIED - LINE - Happy New Year

But you code is doing this

DATE SPECIFIED - LINE - Happy New Year

BETWEEN DATE RANGE SPECIFIED - LINE - Happy New Year

for the specific dates you can build an array and compares today's date to the key
[code=php] $message=array(
'0101'=>' - Happy New Year',
'0101'=>' - Happy Valentines Day',
'0704'=>' - Happy 4th of July',
);
[/code]

Then if nothing is found a different way would be need to see if today falls within a date range.

Alternatively only use a way to look inside a date range and set specific dates as single day date ranges. eg instead of 0704 look to see if today is within 0703 and 0705.

Personally I'd use one mechanism to look for a specific date so you can see that date easily in the code and another to look for date ranges.
Copy linkTweet thisAlerts:
@comptech520authorSep 18.2008 — Please accept my appologies, do you happen to have any examples?
Copy linkTweet thisAlerts:
@SyCoSep 19.2008 — This should do it for you. As the holidays are fast approaching consider it a gift.

If a given day is in the date range then the day message over rides the range message. So happy Holidays displays through the period but Happy New Years day displays on 0101.

It has a quirk in the date ranges, because the code only looks for greater or less than the given date when compared to the start and end date. The issue is the first day of the year is 0101 even though it comes right after 1231, it is in fact greater (in calandar terms) but numerically its less. I worked around it quickly by introducing a requirement for date ranges spanning the new year. The range format includes a -x to identify it so a different part of the logic is entered.

Can anyone say hack ?

Please if you see a neater way post it up.

Just add you holiday dates and messages to the $holidays array and date ranges to the $holiday_ranges (with the -x if required). Call the function and you should be good to go.

And yes St Patricks day is a 3 day holiday (if you do it right)

[code=php]<?

function holidays_message(){
$today=date("md");
//$today='0101';//uncomment line for testing

$holidays=array(
'1231'=>' - Happy New Eve',
'0101'=>' - Happy New Year',
'0214'=>' - Happy Valentines Day',
'0704'=>' - Happy 4th of July'
);

foreach($holidays as $k => $v){
if($today==$k) return $v;
}

//use -x to check date ranges spanning 1231-0101
$holiday_periods=array(
'1115-0112-x'=>'Happy Holidays',
'0316-0318'=>'Happy St Patrick's Day'
);

foreach($holiday_periods as $k => $v){
$dates=explode('-',$k);

//standard date range
if(!isset($dates[2]) && ($today>=$dates[0] && $today<=$dates[1])) return $v;

//date range across 0101 add -x to date range in key
if(isset($dates[2]) && ($today>=$dates[0] && $today<='1231' || $today>='0101' && $today<=$dates[1])) return $v;
}
return;
}

echo holidays_message();[/code]
Copy linkTweet thisAlerts:
@ellisglSep 19.2008 — Why 2 days for St. Patrick's?

http://www.smart.net/~mmontes/ushols.html will give your more holidays, also don't forget holidays which dates change each year.. Look at the end of the page for algorythms.
Copy linkTweet thisAlerts:
@comptech520authorSep 19.2008 — Thank you!

Just one last thing, I added in whats below to create a box around the holiday text. The only thing is if there is no message for the day, the box appears anyway, is there a way to fix that?

Thanks

[code=html]
echo '<DIV style="width:200px; border-style:solid">';
echo holidays_message();
echo '</DIV>';
[/code]
Copy linkTweet thisAlerts:
@SyCoSep 19.2008 — You're welcome. Holidays are coming so I though it might be useful.

Why 2 days for St. Patrick's?[/QUOTE]
Why restrict such a good opportunity to have a drink. You should see how long it takes to bury someone in Donegal!

2 easy ways are; rather than echo the function you can return it to a variable and see if anything is in the variable
[code=php]
$message=holidays_message();
if($message!='') echo '<div>'.$message.'</div>';[/code]


or draw the box in the function.

(In the notes $holiday_ranges should of course have been $holiday_periods)
Copy linkTweet thisAlerts:
@comptech520authorSep 19.2008 — Thanks,

What did you mean by this?

In the notes $holiday_ranges should of course have been $holiday_periods

I'm getting this on line 24: PHP Parse error: syntax error, unexpected T_ECHO
Copy linkTweet thisAlerts:
@SyCoSep 19.2008 — When I posted the code I said this
Just add you holiday dates and messages to the $holidays array and date ranges to the[B] $holiday_ranges[/B] (with the -x if required). Call the function and you should be good to go.[/QUOTE]
I said $holiday_ranges when I should have said $holiday_periods. Just correcting myself.

I'm getting this on line 24: PHP Parse error: syntax error, unexpected T_ECHO[/QUOTE]
If you copy the given code on to a blank page and save it, then visit the URL through the browser you wont get the error As the error message suggests, the echo was unexpected. Your error is just before that. Did you paste this into other code if so we have no way of knowing what the line numbers are now.

In the script I posted line 24 is
[code=php]
foreach($holiday_periods as $k => $v){[/code]


So I know something changed but as I'm neither psychic nor omnipresent I don't know what you've done.
Copy linkTweet thisAlerts:
@SyCoSep 19.2008 — ellisgl Without looking too hard into the class you posted is described as
"This class is able to calculate the dates of certain holidays that occur on variable days of the year."[/QUOTE]
Does it also handle date ranges and days within date ranges?
×

Success!

Help @comptech520 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...