Jump to content


Photo

php date functions, trouble finding last day of month


  • Please log in to reply
2 replies to this topic

#1 juanc

juanc
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 19 August 2006 - 06:33 PM

With regards to the numerical value of each day of the week.

ie. Sunday is 0,Monday is 1,Tuesday is 2 and so on.

I need the value for the day of the week that falls on the last day of the month. For example this month being August ……….so 31 Aug falls on a Thursday so that would be 4

What I’ve written doesn’t work properly.

<?php

$month = date('n');
$year = date('Y');

$timestamp = mktime(0,0,0,$month,1,$year);

$numOfdays = date('t',$timestamp);

$lastDayDate = mktime(0,0,0,$month,$numOfdays,$year);

$lastDayNumber = date('w',$lastDayDate);

echo 'The number is ' . $lastDayNumber;

?>



This actually works but if I change to being in respect this coming September
($timestamp = mktime(0,0,0,9,1,$year);

it gives me 3 when it should be 6 as 30 Sep falls on a Saturday.  Similarly with October I get 4 when it should be 2 as 31 Oct falls on a Tuesday.



#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 19 August 2006 - 07:24 PM

<?php
list($y,$m,$d) = explode("-", date("Y-m-d")); // get current month and year
echo date("w", mktime(0,0,0,$m+1,0,$y)); // day before first day of next month
?>

Legend has it that reading the manual never killed anyone.
My site

#3 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 19 August 2006 - 07:25 PM

Try this...


<?PHP
$mo=8;
$year = 2006;
echo date('w',strtotime($mo . "/" . date('t',strtotime($mo . "/" . "28" . "/" . $year)) . "/" . $year));
?>

Lite...

day late and dollar short

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users