473,372 Members | 1,242 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,372 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 8335
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?

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.