473,386 Members | 2,042 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.

Last month -> Current Month -> Next Month

One
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 :

$next_month = date('m')+1;
$this_month = date('m');
$pre_month = date('m')-1;

However this outputs :
6
06
5

Why does the leading zero get dropped when adding to the date
function ?
Is there a better way to be doing this ?

My select needs to say :
select * from table where date_range is
between ('2008-05%') and ('2008-07%')

Jun 27 '08 #1
3 2995
On 9 Jun, 16:27, One <david.hun...@gmail.comwrote:
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 :

$next_month = date('m')+1;
$this_month = date('m');
$pre_month = date('m')-1;

However this outputs :
6
06
5

Why does the leading zero get dropped when adding to the date
function ?
Because you are not adding to the date function, you are adding to the
OUTPUT of the date function!
Is there a better way to be doing this ?
Yes. See the STR_TO_DATE() function and hen you won't need to use php
for this at all:
http://dev.mysql.com/doc/refman/5.0/...on_str-to-date
Jun 27 '08 #2
NC
On Jun 9, 8:27*am, One <david.hun...@gmail.comwrote:
>
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 :

$next_month = date('m')+1;
$this_month = date('m');
$pre_month = date('m')-1;

However this outputs :
6
06
5

Why does the leading zero get dropped when adding to
the date function ?
Because initially, date() returns a string "06". When you attempt to
add or subtract an integer from that value, it is converted to the
integer 6. The result of the computation is an integer as well.
Is there a better way to be doing this ?
Yes. Here's a possibility:

// Find out current time:
$ts = time();
// Find out what day of the month it is:
$day = date('d', $ts);
// Find out how many days there are in that month:
$days = date('t', $ts);
// Produce the date for the end of the next month:
$next_month = date('Y-m-t', $ts + ($days - $day + 5) * 24 * 60 * 60);
// Produce the date for the beginning of the previous month:
$pre_month = date('Y-m-01', $ts - ($day + 5) * 24 * 60 * 60);
// Create the query:
$query = 'select * from table where date_range is '
. "between ('{$pre_month}') and ('{$next_month}')";

Cheers,
NC
Jun 27 '08 #3
On or about 6/9/2008 11:27 AM, it came to pass that One wrote:
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 :

$next_month = date('m')+1;
$this_month = date('m');
$pre_month = date('m')-1;

However this outputs :
6
06
5

Why does the leading zero get dropped when adding to the date
function ?
Is there a better way to be doing this ?

My select needs to say :
select * from table where date_range is
between ('2008-05%') and ('2008-07%')
Try this
$next_month = str_pad(date('n')+1,2,"0",STR_PAD_LEFT)
$pre_month = str_pad(date('n')-1,2,"0",STR_PAD_LEFT)

You will also need code to check if $next_month is 12
and $pre_month is < 1
in which case you need to adjust both month and year respectively.
Jun 27 '08 #4

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

Similar topics

1
by: Stanley | last post by:
Ok I am building a calendar in my code and when it is rendered I use the DayRender event to add links to each day. However, I can't seem to find a way to build links for the Prev and Next month...
0
by: Alessandro | last post by:
There is a way to "disable" or remove button to go to next month ? I want, for example, that a user can move from 01/01/2003 to 31/12/2003 but not in 2004..... Any idea ?
13
by: Mark | last post by:
Dear folks, In Javascript, is it possible to get all id names within, say, a <div></divpair? Like the array of "document.images", I mean. The reason I ask, is that I have a calender whose...
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...
6
by: Yinghe Chen | last post by:
Hi, Could someone help on how to use python to output the next month string like this? "AUG07", suppose now is July 2007. I think also need to consider Dec 07 case, it is supposed to output...
2
by: ncsthbell | last post by:
I am having problems getting the end date to calculate correctly. I start with Quarter '03/02', (YY/QTR), for this it means it is for the 2nd qtr of 2003. My goal is to get the begin & end dates...
2
parshupooja
by: parshupooja | last post by:
Hey, I have dropdownlist where I am adding 2 values: DDCycle.Items.Add(new ListItem(DateTime.Now.AddMonths(-1).ToString("MMMM"), DateTime.Now.AddMonths(-1).ToString("M/yy")));...
1
by: Vikas Jain | last post by:
hi, please tell me how to subtract month from current date. Thanks in advance. Vikas Jain
14
by: yappy77 | last post by:
I have a query that I need help in writing the expression to calculate the first of the month following the end of previous month. Example: Policy expiration date = between 2/1/11 and 2/28/11...
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: 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: 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
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...

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.