Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

PHP - IF ELSE according to year in date field

Explorer ,
Mar 04, 2009 Mar 04, 2009
I'd like to define a variable according to which year appears in a date selected from a drop down menu populated from a recordset.
At the moment I have this code as a rough idea:

if ($row_rs_dates['start_date'] = %,%,2009)
{
$coursefee=$row_rs_course['cost09'];
}
else
{$coursefee=$row_rs_course['cost10'];
}

It's the %,%,2009 bit that needs to be replaced with something that detects only the year contained within the MySql date field and sets $coursefee to cost09 or cost10 accordingly.

Can anyone help me out?

Cheers


Dave
TOPICS
Server side applications
378
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 04, 2009 Mar 04, 2009
mac-in-the-mountains wrote:
> It's the %,%,2009 bit that needs to be replaced with something that detects
> only the year contained within the MySql date field and sets $coursefee to
> cost09 or cost10 accordingly.

It depends on how the date is being returned from MySQL. The default
format for a date in MySQL is YYYY-MM-DD, so you can just check the
first four digits like this:

if (substr($row_rs_dates['start_date'], 0, 4) == '2009') {
$coursefee = $row_rs_course['cost09'];
} else {
$coursefee = $row_rs_course['cost10'];
}

However, the disadvantage with this is that you're going to need to
change your code every year. A better solution would be to use the final
two digits of the year like this:

$year = substr($row_rs_dates['start_date'], 2, 2);
$coursefee = $row_rs_course["cost$year"];

Note that I have used double quotes for "cost$year" to ensure that the
value of $year is substituted.

There are other solutions, particularly if you have used DATE_FORMAT()
to format the date as it's retrieved from MySQL. For example, you could
use DATE_FORMAT(start_date, '%y') AS theYear to extract the final two
digits of the year from the start_date column.

--
David Powers
Adobe Community Expert, Dreamweaver
http://foundationphp.com
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 04, 2009 Mar 04, 2009
On 04 Mar 2009 in macromedia.dreamweaver.appdev, David Powers wrote:

> It depends on how the date is being returned from MySQL. The default
> format for a date in MySQL is YYYY-MM-DD, so you can just check the
> first four digits like this:
>
> if (substr($row_rs_dates['start_date'], 0, 4) == '2009') {

Even faster and easier is using MySQL's year() function:

if (year($row_rs_dates['start_date']) == '2009') {

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_year

--
Joe Makowiec
http://makowiec.net/
Email: http://makowiec.net/contact.php
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 04, 2009 Mar 04, 2009
LATEST
Joe Makowiec wrote:
> Even faster and easier is using MySQL's year() function:
>
> if (year($row_rs_dates['start_date']) == '2009') {

But not like that. It needs to be used as part of the SQL query. It
won't work inside the PHP code. PHP doesn't have a year() function.

If the date is already being formatted with DATE_FORMAT(), I agree that
using YEAR() to extract the year from the date is probably a good idea.

However, the OP needs only the last two digits of the year, so adding
this to the SQL query does it in one:

DATE_FORMAT(start_date, '%y') AS theYear

--
David Powers
Adobe Community Expert, Dreamweaver
http://foundationphp.com
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines