469,308 Members | 2,691 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,308 developers. It's quick & easy.

getting first day and last day of month

Hi, Using PHP 4, if I have a date, what is a function I could use to
give me a date that represents the first day of that month? For
example, if my date were "3/19/2006 8:00", I would want my function to
return "3/1/2006 8:00". Similarly what function would I use to return
the last day of the month? In the above example, the output I would
want returned is "3/31/2006 8:00".

Thanks, -

Mar 2 '06 #1
4 47933
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

la***********@zipmail.com wrote:
if my date were "3/19/2006 8:00", I would want my function to
return "3/1/2006 8:00".


See date() and mktime().

- --
- ----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

http://acm.asoc.fi.upm.es/~mr/ ; http://acm.asoc.fi.upm.es/~ivan/
MSN:i_*************************@hotmail.com
Jabber:iv*********@jabber.org ; iv*********@kdetalk.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFEB4tZ3jcQ2mg3Pc8RApx8AJ41L8w5nYc4Ugm0cA95tD BiFNiDuwCcD4/E
36+mLMZpQckMv64n1ja7aaE=
=gKuM
-----END PGP SIGNATURE-----
Mar 3 '06 #2

la***********@zipmail.com wrote:
Hi, Using PHP 4, if I have a date, what is a function I could use to
give me a date that represents the first day of that month? For
example, if my date were "3/19/2006 8:00", I would want my function to
return "3/1/2006 8:00". Similarly what function would I use to return
the last day of the month? In the above example, the output I would
want returned is "3/31/2006 8:00".


The first date of the month is trivial: replace the day number with 1.
The last day of the month is somewhat harder, because this varies with
months and leap years. However, you could use checkdate to figure out
what the last day of the month is:

function getlastdayofmonth($month, $year) {
for ($day = 28; $day < 32; $day++) {
if (!checkdate($month, $day, $year)) return $day-1;
}
}

Mar 3 '06 #3
Sjoerd said the following on 03/03/2006 10:58:
la***********@zipmail.com wrote:
Hi, Using PHP 4, if I have a date, what is a function I could use to
give me a date that represents the first day of that month? For
example, if my date were "3/19/2006 8:00", I would want my function to
return "3/1/2006 8:00". Similarly what function would I use to return
the last day of the month? In the above example, the output I would
want returned is "3/31/2006 8:00".


The first date of the month is trivial: replace the day number with 1.
The last day of the month is somewhat harder, because this varies with
months and leap years. However, you could use checkdate to figure out
what the last day of the month is:

function getlastdayofmonth($month, $year) {
for ($day = 28; $day < 32; $day++) {
if (!checkdate($month, $day, $year)) return $day-1;
}
}


Or more directly:

function getLastDayOfMonth($month, $year)
{
return idate('d', mktime(0, 0, 0, ($month + 1), 0, $year));
}

--
Oli
Mar 3 '06 #4
Sjoerd wrote:
la***********@zipmail.com wrote:
Hi, Using PHP 4, if I have a date, what is a function I could use to
give me a date that represents the first day of that month? For
example, if my date were "3/19/2006 8:00", I would want my function to
return "3/1/2006 8:00". Similarly what function would I use to return
the last day of the month? In the above example, the output I would
want returned is "3/31/2006 8:00".

The first date of the month is trivial: replace the day number with 1.
The last day of the month is somewhat harder, because this varies with
months and leap years. However, you could use checkdate to figure out
what the last day of the month is:

function getlastdayofmonth($month, $year) {
for ($day = 28; $day < 32; $day++) {
if (!checkdate($month, $day, $year)) return $day-1;
}
}


Or, once you have the first of the month, add one month and subtract one
day.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 3 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Vijay Balki | last post: by
reply views Thread by bdtmike | last post: by
2 posts views Thread by Janick Bernet | last post: by
3 posts views Thread by One | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.