Lost Password?

Go Back   CodeCall Programming Forum > Web Development Forum > PHP Forum

PHP Forum Use this forum to discuss all aspects of PHP Development. PHP is a server-side, cross-platform, HTML embedded scripting language that lets you create dynamic web pages.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-20-2006, 11:17 AM
Chan Chan is offline
Programming Professional
 
Join Date: Jun 2006
Posts: 204
Rep Power: 9
Chan is on a distinguished road
Default Find last Monday

How can I find the date of the most recent Monday in PHP?

If it were Wensday, 11/15/2006 I would want to have 11/13/2006 as the last monday.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 11-20-2006, 04:50 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,731
Last Blog:
Passwords
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John
Default

PHP Code:
echo date('m/d/Y'strtotime('-2 days')); 
of course you would have to do like

PHP Code:
//if today is monday
echo date('m/d/Y');

//if today is tuesday
echo date('m/d/Y'strtotime('-1 days'));

//if today is wednessday
echo date('m/d/Y'strtotime('-2 days'));

//if today is thursday
echo date('m/d/Y'strtotime('-3 days')); 

Last edited by John; 11-20-2006 at 04:53 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 11-21-2006, 10:30 AM
Chan Chan is offline
Programming Professional
 
Join Date: Jun 2006
Posts: 204
Rep Power: 9
Chan is on a distinguished road
Default

So I would have to do a bunch of ifs and hard code it? Isn't there a better way to do it?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 11-30-2006, 12:38 PM
awgeorge awgeorge is offline
Newbie
 
Join Date: Nov 2006
Age: 17
Posts: 1
Rep Power: 0
awgeorge is on a distinguished road
Default There is a better way

Hi, instead of using (-2) you can atually use words if you wanted the last monday, all you would have to do is put "Last Monday":

Like this

PHP Code:
$monday date('m/d/Y'strtotime('last monday'));  //last moday
$tuesday date('m/d/Y'strtotime('last tuesday')); 
$wednesday date('m/d/Y'strtotime('last wednesday')); 
$thursday date('m/d/Y'strtotime('last thursday')); 
$friday date('m/d/Y'strtotime('last friday')); // last friday

// Then all you would do is echo it.

echo $monday;

// and so on 
You can also put times into it like this

PHP Code:
$when 'next thursday'// what date its aming for
$sethour '12'// Set hour
$setminute '30'// Set minutes
$hourminus2 $sethour-2// For the strotime function

$deadline strtotime("".$when." at ".$hourminus2.":".$setminute.""); // set a deadline

print strftime('%c',$deadline) ; 
it displays Thu Nov 30 12:30:00 2006 but because of some php errors you have to minus 2 hours. so thats what i have done above.

Will George

Last edited by awgeorge; 11-30-2006 at 12:44 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 12-04-2006, 11:18 AM
Chan Chan is offline
Programming Professional
 
Join Date: Jun 2006
Posts: 204
Rep Power: 9
Chan is on a distinguished road
Default

Thank you for your input! I actually "borrowed" the code from the community project Weekly Stats:

PHP Code:
// Variables
$current_date time();

// Get day of the week
$current_day dayofweek();

// Find last Monday
$last_monday mktime(0,0,0date("m"$current_date), date("d"$current_date)-($current_day-1), date("Y"$current_date)); 
And the day function:
PHP Code:
/**********************************************************
* Function retuns the day of the week as an integer.
* Used to find the last Monday
**********************************************************/
function dayofweek()
{
        
$days = array("Mon""Tue""Wed""Thu""Fri""Sat""Sun");
        return 
array_search(date("D"), $days) + 1;

Thanks!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find derivative in java sania21 Java Help 1 05-15-2007 06:35 AM
How to find out which record the user clicked? reachpradeep Perl 1 03-03-2007 02:24 PM
A great find: Beginning Perl techni68 Perl 0 01-21-2007 05:39 AM
Find Date/Time Nightracer Database & Database Programming 3 01-04-2007 04:58 PM
Where to find PHP tutorials Jordan PHP Forum 1 11-09-2005 01:23 PM


All times are GMT -5. The time now is 03:41 AM.

Contest Stats

John ........ 223.00000
dargueta ........ 168.00000
Xav ........ 164.00000
LogicKills ........ 20.00000
gaylo565 ........ 18.00000
WingedPanther ........ 15.00000
|pH| ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 67%

Ads