[SOLVED] HELP NEEDED - Date Manipulation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

HELP NEEDED - Date Manipulation

Post by RobertGonzalez »

Can someone point me in the right direction on this question? I am building a "weekly message" CMS for a church web site. Their week starts on Sunday and they want to be able to post the Pastors sermon (or notes) for this weeks service on their web site.

What I would like to do is have the page load the message that falls on last Sunday all week long (until the next Sunday, when it loads the next week's message). I would also like to title the page "Weekly Message for Mmm, dd, YYYY" where the date is the most recent Sunday. All of the data will be stored in a MySQL table (unless the printed weekly date does not need to be).

I am drawing a total blank on how to do this. All help would be most appreciated.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

<?php

	echo date('M d, Y H:i:s', strtotime('last sunday'));

?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

OK, lets take it a step further. In the CMS I want to allow the admin to select a Sunday from a select option list. The list should contain all sundays for the year. I was thinking of using a for loop with the start pointer as 'Next Sunday', but I am not sure how to set the constraint parameter to keep the date list to this year only. Any suggestions?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

why not have a loop check the "current" year against the year you are asking for timestamps for?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Helpful research links: [ [google]php date manipulation[/google] ]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

[SOLVED] - Date Manipulation

Post by RobertGonzalez »

I figured out what I want to do. Sorry for not posting back sooner. This is what I was looking for:

Code: Select all

<?php
echo '<select name="date">';
for ($date = strtotime('next Sunday'); $date < mktime(0,0,0,1,1,(date('Y')+1)); $date + 604800)
{
    echo '<option value="' . $date . '">' . date('l, F jS, Y', $date) . '</option>';
}
echo '</select>';

?>
I hope this helps someone out. This returns all Sundays with a unixtimestamp less than the first Sunday of Next Year.
Post Reply