Jump to content

[SOLVED] Date in PHP


am_25

Recommended Posts

thanks

But one more question. Today's date is 20th tuesday.

The week starts from 19th Mon to 25th Sun

I want it to be displayed like that

For eg. In case the date is 23rd feb,

it should display from 19th to 25th

 

How to do that?

thanks

 

Link to comment
Share on other sites

<?php
function showWeek($startDay, $start=NULL){
   if(!$start){
      $start = time();
   }
   while(date("w") != $startDay){
      $start = $start-(60*60*24);
   }

   for($i=0; $i<7; $i++){
      $date = mktime(0,0,0, date("m", $start), (date("d", $start)+$i), date("Y", $start));
      print date("Y-m-d", $date).'<br />';
   }
}

showWeek(1);
?>

Should start on Monday. I made it a little more abstract for you.

Link to comment
Share on other sites

I'm not aware of any quick and easy function that will let you do this, but you can work it out with a bit of math.

 

Begin by determining what the first day of the month is with something like:

 

$firstday = date('D', mktime(0,0,0, date('m'), 1, date('Y')));

 

Then you set up an offset value for the first day of the week based on $firstday, using a switch:

 

switch($firstday)
{
case 'Mon':
$offset = 1;
break;

case 'Tue':
$offset = 2;
break;

case 'Wed':
$offset = 3;
break;

case 'Thu':
$offset = 4;
break;

case 'Fri':
$offset = 5;
break;

case 'Sat':
$offset = 6;
break;

case 'Sun':
$offset = 7;
break;
}

 

Then you can establish the startday of each week:

 

$week1 = 1 + $offset;
$week2 = $week1 + 7;
$week3 = $week2 + 7;
$week4 = $week3 + 7;

 

You will have to further refine it for the end of the month, but this should be enough to get you going.

 

Good luck!

 

Trev

Link to comment
Share on other sites

This code works well.

 

$StartOfWeek = date("Y-m-d",mktime(0,0,0,date("n"),(date("j")-date("w")),date("Y")));

$EndOfWeek = date("Y-m-d",mktime(23,59,59,date("n"),(date("j")+(6-date("w"))),date("Y")));

echo $StartOfWeek;

print "<br>";

echo $EndOfWeek;

 

Thanks all.

 

 

Link to comment
Share on other sites

with this code how to get the dates for the entire week

 

$StartOfWeek = date("Y-m-d",mktime(0,0,0,date("n"),(date("j")-date("w")),date("Y")));

$EndOfWeek = date("Y-m-d",mktime(23,59,59,date("n"),(date("j")+(6-date("w"))),date("Y")));

echo $StartOfWeek;

print "<br>";

echo $EndOfWeek;

Link to comment
Share on other sites

thanks

But one more question. Today's date is 20th tuesday.

The week starts from 19th Mon to 25th Sun

 

That can begin to be a bit tricky since the week realistically starts on Sunday and runs through Saturday. I would recommend something like this as an alternative to jesi's code, if you are only after displaying the current week:

<?php
function displayWeek() {
  if (date('w') == 0) { // today is sunday, so get LAST monday's date to start with
    $start = strtotime("last Monday");
  } else { // this monday will give us the right starting date
    $start = strtotime("Monday");
  }

  $dates = array();
  // Loop for 7 days
  for ($i = 0; $i < 7; $i++) {
    $dates[] = date('Y-m-d', strtotime("$start +{$i} days"));
  }

  return $dates;
}

$dates = displayWeek();
echo implode("<br />", $dates);
?>

Link to comment
Share on other sites

I dunno

why I see a contradiction between:

I dunno Ted, maybe the manual has a list of the syntax it supports...

AND

Thanks Obsidian, I always forget about how strtotime can handle stuff like "Last Monday" - that makes it cleaner and shorter than mine.

Can any body suggest why that is :-\

Ted

Link to comment
Share on other sites

$start = strtotime("last Monday"); ??? i never know it does that, what other text does it read?

 

Test it and see... I've been amazed at the different things I've been able to use it for:

strtotime — Parse about any English textual datetime description into a Unix timestamp

Link to comment
Share on other sites

I dunno

why I see a contradiction between:

I dunno Ted, maybe the manual has a list of the syntax it supports...

AND

Thanks Obsidian, I always forget about how strtotime can handle stuff like "Last Monday" - that makes it cleaner and shorter than mine.

Can any body suggest why that is :-\

Ted

 

LOL... just remember there's a difference between being able to spout off a list of all available syntax formats for a function and not remembering one that you learned a while back ;)

Link to comment
Share on other sites

Because I didn't ask other people a question that can be answered by looking in the manual - I simply forgot how to do something which was available and did it another way. You asked us for information which was available, you didn't post another way to do it.

 

Edit: plus what Obsidian said.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.