473,406 Members | 2,387 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,406 software developers and data experts.

first day of month from sql date.

If I have an sql date, such as "2006-11-19" is there a way using either
sql or php date and time functions to get the date of the the first day
of the month ?
Nov 20 '06 #1
10 8338
You could do something like this:

<?

function firstDayOfMonth($str = '2006-11-19')
{
$day = date('l', strtotime(date('Y/m/01', strtotime($str))));
return $day;
}

echo firstDayOfMonth();

?>

Change the date format string if you want other information about the
first day of the month.

meltedown wrote:
If I have an sql date, such as "2006-11-19" is there a way using either
sql or php date and time functions to get the date of the the first day
of the month ?
Nov 20 '06 #2
petersprc wrote:
You could do something like this:

<?

function firstDayOfMonth($str = '2006-11-19')
{
$day = date('l', strtotime(date('Y/m/01', strtotime($str))));
return $day;
}

echo firstDayOfMonth();

?>

Change the date format string if you want other information about the
first day of the month.

meltedown wrote:
>If I have an sql date, such as "2006-11-19" is there a way using either
sql or php date and time functions to get the date of the the first day
of the month ?
Thanks, but I want the date, not the weekday. For 2006-11-19 it would be
2006-11-01
Nov 20 '06 #3
You could work on the text:

$date = '2006-11-19';
$newDate = substr($date, 0, strrpos($date, '-')) . '-01';

meltedown wrote:
petersprc wrote:
You could do something like this:

<?

function firstDayOfMonth($str = '2006-11-19')
{
$day = date('l', strtotime(date('Y/m/01', strtotime($str))));
return $day;
}

echo firstDayOfMonth();

?>

Change the date format string if you want other information about the
first day of the month.

meltedown wrote:
If I have an sql date, such as "2006-11-19" is there a way using either
sql or php date and time functions to get the date of the the first day
of the month ?

Thanks, but I want the date, not the weekday. For 2006-11-19 it would be
2006-11-01
Nov 20 '06 #4
petersprc wrote:
You could work on the text:

$date = '2006-11-19';
$newDate = substr($date, 0, strrpos($date, '-')) . '-01';

meltedown wrote:
>petersprc wrote:
>>You could do something like this:

<?

function firstDayOfMonth($str = '2006-11-19')
{
$day = date('l', strtotime(date('Y/m/01', strtotime($str))));
return $day;
}

echo firstDayOfMonth();

?>

Change the date format string if you want other information about the
first day of the month.

meltedown wrote:
If I have an sql date, such as "2006-11-19" is there a way using either
sql or php date and time functions to get the date of the the first day
of the month ?
Thanks, but I want the date, not the weekday. For 2006-11-19 it would be
2006-11-01
Yes I know but I'd rather not.
Nov 20 '06 #5

meltedown wrote:
petersprc wrote:
You could work on the text:

$date = '2006-11-19';
$newDate = substr($date, 0, strrpos($date, '-')) . '-01';

meltedown wrote:
petersprc wrote:
You could do something like this:

<?

function firstDayOfMonth($str = '2006-11-19')
{
$day = date('l', strtotime(date('Y/m/01', strtotime($str))));
return $day;
}

echo firstDayOfMonth();

?>

Change the date format string if you want other information about the
first day of the month.

meltedown wrote:
If I have an sql date, such as "2006-11-19" is there a way using either
sql or php date and time functions to get the date of the the first day
of the month ?
Thanks, but I want the date, not the weekday. For 2006-11-19 it would be
2006-11-01

Yes I know but I'd rather not.
This from Azizur Rahman at

http://dev.mysql.com/doc/refman/5.0/...functions.html

To get the first day of the current month:

SELECT ((PERIOD_ADD(EXTRACT(YEAR_MONTH FROM CURDATE()),0)*100)+1) as
FirstDayOfTheMonth;

Nov 20 '06 #6
meltedown wrote:
If I have an sql date, such as "2006-11-19" is there a way using either
sql or php date and time functions to get the date of the the first day
of the month ?
select date_sub(YOUR_DATE_COLUMN, interval day(YOUR_DATE_COLUMN)-1 day)
from YOUR_TABLE;

--
I (almost) never check the dodgeit address.
If you *really* need to mail me, use the address in the Reply-To
header with a message in *plain* *text* *without* *attachments*.
Nov 20 '06 #7
"meltedown" <as**@fake.comwrote in message
news:6k*****************@fe03.news.easynews.com...
petersprc wrote:
>You could do something like this:

<?

function firstDayOfMonth($str = '2006-11-19')
{
$day = date('l', strtotime(date('Y/m/01', strtotime($str))));
return $day;
}

echo firstDayOfMonth();

?>

Change the date format string if you want other information about the
first day of the month.

meltedown wrote:
>>If I have an sql date, such as "2006-11-19" is there a way using either
sql or php date and time functions to get the date of the the first day
of the month ?

Thanks, but I want the date, not the weekday. For 2006-11-19 it would be
2006-11-01
Well the answer was already given in petersprc's first post, you just need a
little part of it:

date('Y-m-01', strtotime($str)); // this takes given timestamp $str and
outputs a Y-m-d formatted date for first of the month just as you wanted.

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Nov 20 '06 #8
meltedown wrote:
petersprc wrote:
>You could work on the text:

$date = '2006-11-19';
$newDate = substr($date, 0, strrpos($date, '-')) . '-01';

meltedown wrote:
>>petersprc wrote:

You could do something like this:

<?

function firstDayOfMonth($str = '2006-11-19')
{
$day = date('l', strtotime(date('Y/m/01', strtotime($str))));
return $day;
}

echo firstDayOfMonth();

?>

Change the date format string if you want other information about the
first day of the month.

meltedown wrote:

If I have an sql date, such as "2006-11-19" is there a way using
either
sql or php date and time functions to get the date of the the first
day
of the month ?

Thanks, but I want the date, not the weekday. For 2006-11-19 it would be
2006-11-01


Yes I know but I'd rather not.
Why not? It's probably the most efficient way of doing it.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Nov 20 '06 #9
Jerry Stuckle wrote:
meltedown wrote:
>petersprc wrote:
>>You could work on the text:

$date = '2006-11-19';
$newDate = substr($date, 0, strrpos($date, '-')) . '-01';

meltedown wrote:

petersprc wrote:

You could do something like this:
>
<?
>
function firstDayOfMonth($str = '2006-11-19')
{
$day = date('l', strtotime(date('Y/m/01', strtotime($str))));
return $day;
}
>
echo firstDayOfMonth();
>
?>
>
Change the date format string if you want other information about the
first day of the month.
>
meltedown wrote:
>
>If I have an sql date, such as "2006-11-19" is there a way using
>either
>sql or php date and time functions to get the date of the the
>first day
>of the month ?

Thanks, but I want the date, not the weekday. For 2006-11-19 it
would be
2006-11-01


Yes I know but I'd rather not.

Why not? It's probably the most efficient way of doing it.
Could be, and it could be fine, but it treats a date datatype as a
string datatype. I'd rather stick to strict datatypes which to me means
its less likely to have some unforseen problem. I guess its just a
habit I got from starting out with C++
Nov 21 '06 #10
meltedown wrote:
Jerry Stuckle wrote:
>meltedown wrote:
>>petersprc wrote:

You could work on the text:

$date = '2006-11-19';
$newDate = substr($date, 0, strrpos($date, '-')) . '-01';

meltedown wrote:

petersprc wrote:
>
>You could do something like this:
>>
><?
>>
>function firstDayOfMonth($str = '2006-11-19')
>{
> $day = date('l', strtotime(date('Y/m/01', strtotime($str))));
> return $day;
>}
>>
>echo firstDayOfMonth();
>>
>?>
>>
>Change the date format string if you want other information about the
>first day of the month.
>>
>meltedown wrote:
>>
>>If I have an sql date, such as "2006-11-19" is there a way using
>>either
>>sql or php date and time functions to get the date of the the
>>first day
>>of the month ?
>
>
Thanks, but I want the date, not the weekday. For 2006-11-19 it
would be
2006-11-01


Yes I know but I'd rather not.


Why not? It's probably the most efficient way of doing it.

Could be, and it could be fine, but it treats a date datatype as a
string datatype. I'd rather stick to strict datatypes which to me means
its less likely to have some unforseen problem. I guess its just a
habit I got from starting out with C++
I understand, and that's a very valid reason.

But in PHP there is no date datatype - unless you create a date class
yourself. It's handled as a string.

If you want to be really good about it, you could create a date class
and have it parse out the date from MySQL. I've done this before when
I've needed to do a bunch of stuff on dates. It did help simplify the
rest of the code (which a properly constructed class should do). But it
also added a little extra overhead to the process (which was also expected).

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Solomon Grundy | last post by:
Select trunc(sysdate,'MM') from dual of course gives me the first day of the month, may I ask what the query is to find the first saturday of the month?
3
by: johkar | last post by:
I need to document.write out a select list populated with the dates for the first and third Wednesday of each month. How do I get to the actual days? <select name="mySelect"> <option value="Oct...
5
by: Parintas Themis STE Kardias | last post by:
Hi How can i have in a textbox the first sunday of a month
4
by: laredotornado | last post by:
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...
18
by: ben.carbery | last post by:
Hi, I have just written a simple program to get me started in C that calculates the number of days since your birthdate. One thing that confuses me about the program (even though it works) is...
44
by: user | last post by:
Hi, Let's say I have 2 dates in the b/m format: Date 1 and date 2 How do I check whether Date2 is later than Date 1? Date1. 21-Nov-2006 09:00:00 PM
4
by: perryclisbee via AccessMonster.com | last post by:
I have dates of service for several people that range all over each month. ie: patient had dates of service of: 7/3/2006, 7/24/2006 and 7/25/2006. I need to create a new field via a query that...
5
by: tolcis | last post by:
Hi! I have a query that has to return bunch of data based on the calendar month. I have to make sure that it will return data to me for 28 days if it is February and for 31 if it is August(for...
6
by: =?Utf-8?B?UGF1bA==?= | last post by:
HI I have a stored procedure that returns data with a date field in the form of a DateTime type. I need to place data in variables based on days of the week starting with the first thursday of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.