469,289 Members | 2,330 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 8117
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Solomon Grundy | last post: by
3 posts views Thread by johkar | last post: by
5 posts views Thread by Parintas Themis STE Kardias | last post: by
4 posts views Thread by laredotornado | last post: by
4 posts views Thread by perryclisbee via AccessMonster.com | last post: by
6 posts views Thread by =?Utf-8?B?UGF1bA==?= | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.