473,386 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 48191
-----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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kate | last post by:
Hi... I have a foxpro program which is for producing excel reports back ended to foxpro table(.dbf). Since the table is getting bigger and bigger, it slows down my program. I need a program...
2
by: Vijay Balki | last post by:
I need to view a discussion posted last month in this group. How do I see it? VJ
4
by: Anon | last post by:
Hello All! I have written a simple app that auto downloads a file from a secure ftp, renames it, and moves it to a network location. Everything works great except the renaming part. I parse out...
0
by: bdtmike | last post by:
I'm using the GridView control and have the Mode of the Pager set to "NumericFirstLast". However, when there are several pages to display, the pager shows only page numbers--and no First/Last...
2
by: Tony Ciconte | last post by:
Does anyone know of or have any VBA code or similar logic that can help distinguish similar first/last name combinations? For example, we would like to prompt the user of a possible match when any...
2
by: Janick Bernet | last post by:
I just stumbled upon this olap function and wondered why the "NULLS FIRST/LAST" clause is not possible in normal order by but only using row_number() over(). Sure, I can do a SELECT *,...
3
by: remya1000 | last post by:
i'm using ASP with MSAccess as database. i have two buttons and two textbox in my page. when i press my first button (First month) i need to display the current month in one textbox and last one...
3
by: One | last post by:
Hi group - I want to do a SELECT based on a date range - but mySQL syntax expects the month range to be two digits. So I have converted the month to show the leading zero like this : ...
3
by: Coolboy55 | last post by:
Hi everyone, I'm having some trouble with a query. I have a history table where one of the fields is a date stamp. My query takes a date from the user, and I want the query to return all records...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.