Get Date for Monday and Friday for the Current Week (Php)

Get date for monday and friday for the current week (PHP)

These strtotime inputs work very well:

strtotime( "next monday" );
strtotime( "previous monday" );
strtotime( "today" );
strtotime( "next friday" );
strtotime( "previous friday" );

All you need to do is to wrap the logic inside some if statements.

STRTOTIME to find current week, date for Monday and Saturday, PHP

why don't you try like this


//check the current day
if(date('D')!='Mon')
{
//take the last monday
$staticstart = date('Y-m-d',strtotime('last Monday'));

}else{
$staticstart = date('Y-m-d');
}

//always next saturday

if(date('D')!='Sat')
{
$staticfinish = date('Y-m-d',strtotime('next Saturday'));
}else{

$staticfinish = date('Y-m-d');
}

Calculate date for Monday of current week

wont this work?

echo date("d m Y",strtotime('monday this week'));

how do i get 5 dates of current week with php or jquery?

An easier way to get those dates might be something like this:

$today = time();
$wday = date('w', $today);
$datemon = date('m-d-Y', $today - ($wday - 1)*86400);
$datetue = date('m-d-Y', $today - ($wday - 2)*86400);
$datewed = date('m-d-Y', $today - ($wday - 3)*86400);
$datethu = date('m-d-Y', $today - ($wday - 4)*86400);
$datefri = date('m-d-Y', $today - ($wday - 5)*86400);

I would also recommend using an array instead of 5 different variables, but that's a separate concern.

getting the date of monday and friday from next week

DateTime MyDate = DateTime.Now;
DateTime NextMondayDate;
DateTime NextFridayDate;
Boolean Test = true;
while (Test)
{
if (MyDate.DayOfWeek.ToString().ToUpper() == "MONDAY")
{
NextMondayDate = MyDate;
NextFridayDate = MyDate.AddDays(4);
Test = false;
}

else if (MyDate.DayOfWeek.ToString().ToUpper() == "FRIDAY")
{
NextFridayDate = MyDate;
NextMondayDate = MyDate.AddDays(3);
Test = false;
}
else
{
MyDate = MyDate.AddDays(1);
}

}

Get date of every monday of current week and the past 8 weeks?

You said:

i want to get the date of every monday of current month and last 8 weeks

So, using DateTime and strtotime

    $date = new \DateTime;
$weekDay = 'Monday';
$ts = strtotime('first day of next month');
$date->setTimestamp($ts);
$month = $date->format('m');
$thisMonthTs = strtotime('first day of this month');
$date->setTimestamp($thisMonthTs);
$thisMonth = $date->format('m');

while ($month >= $thisMonth) {
$ts = strtotime("previous $weekDay", $ts);
$date->setTimestamp($ts);
$month = $date->format('m');
echo $date->format('d-m-Y');
echo '<br>';
}

for ($n = 0; $n < 7; $n++) {
$ts = strtotime("previous $weekDay", $ts);
$date->setTimestamp($ts);
echo $date->format('d-m-Y');
echo '<br>';
}

Solution may be improved, because it is a quick-trick. But all what you need it is datetime and strtotime

Get current week start/end date with DST

I am aware that there is more than one way to skin a cat, but in this case I was interested in how to fix my current code and more importantly to find out what's wrong with it.

Thank you all for your suggestions, especially to @misorude for pointing out the obvious flaw in my initial code, whereas "not every day has 86400 seconds", which is especially true during DST.

So here's the updated working code using relative "days" instead of fixed seconds amount:

$monday = strtotime('next Monday -1 week');
$monday = date('w', $monday)==date('w') ? strtotime(date("Y-m-d",$monday)." +7 days") : $monday;
$sunday = strtotime(date("Y-m-d",$monday)." +6 days");
echo "This week start/end date:<br>";
echo $this_week_sd = date("Y-m-d",$monday)."<br>";
echo $this_week_ed = date("Y-m-d",$sunday)."<br>";

//output:
This week start/end date:
2018-10-29
2018-11-04

Once again, thank you all for your inputs. Much appreciated!



Related Topics



Leave a reply



Submit