Crazy Horse
asked on
How to add month values to a drop down menu that can be sorted by month from MYSQL
How would I modify this to store months in a database so that I could sort the data later by month. There will only be one set of records per month.
If I do this and enter March's data first, then when using ORDER BY month DESC, it starts with March instead of January. Should I just replace the values with 1, 2, 3.....12 or is there a better way?
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
If I do this and enter March's data first, then when using ORDER BY month DESC, it starts with March instead of January. Should I just replace the values with 1, 2, 3.....12 or is there a better way?
ASKER
Thanks, Ray. I was actually reading the first link you posted there. I am looking for a way to then change the 01 to Jan, 02 to Feb etc. Will take a look...
You could use an array to translate these values, or you could use a PHP date algorithm.
The translation array would look something like [ '01' => 'Jan', '02' => 'Feb', ... ]
The algorithmic translation would look something like this
The translation array would look something like [ '01' => 'Jan', '02' => 'Feb', ... ]
The algorithmic translation would look something like this
$num = '02'; // FROM THE REQUEST SELECTION
$month = date('M', strtotime("2000-$num-01"));
I am inclined to choose the algorithmic translation. It will avoid some of the risks of loose data types in PHP, where a string '02' can be recast into an integer 2, with awkward results when used as an array index.
ASKER
Is something like this okay? Seems like I already have this in my helpers function file. I have been chasing my tail and I had this function all along. Sigh.
It looks similar-ish to what you posted.
function monthFormat($month) {
$month = strtotime($month);
return date('F', $month);
}
It looks similar-ish to what you posted.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yeah, can't remember which way around it is off hand but one will give you JAN and the other JANUARY. (I think)
Hmm. I tried the function I posted and it is working and I am doing what you suggested and storing as 01, 02, 03 etc.
Hmm. I tried the function I posted and it is working and I am doing what you suggested and storing as 01, 02, 03 etc.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I tried the function I posted and it is working ...
??
https://iconoun.com/demo/temp_black_sulfur.php<?php // demo/temp_black_sulfur.php
/**
* https://www.experts-exchange.com/questions/29019949/How-to-add-month-values-to-a-drop-down-menu-that-can-be-sorted-by-month-from-MYSQL.html#a42117128
*/
error_reporting(E_ALL);
echo '<pre>';
// FROM THE AUTHOR'S POST AT E-E
function monthFormat($month) {
$month = strtotime($month);
return date('F', $month);
}
$months = [ '01', '02', '03', '04', '12' ];
foreach ($months as $month)
{
echo PHP_EOL . $month . ' ';
echo monthFormat($month);
}
Outputs
01 December
02 December
03 December
04 December
12 December
Try the function here, and see if that doesn't work better.
ASKER
@Ray,
Sorry, I was mistaken. I only had one record which was 01 and it was showing "January" so it appeared to be working. After adding 02 and 03 I was getting a list of 3 x January so clearly it didn't work. Apologies!
Sorry, I was mistaken. I only had one record which was 01 and it was showing "January" so it appeared to be working. After adding 02 and 03 I was getting a list of 3 x January so clearly it didn't work. Apologies!
^^ No problem -- that's why we write code that can test our code!
The reason your example said January and mine said December goes to the issue of the default timezone settings. The return from failing strtotime() is FALSE, which is loosely interpreted to be zero. The "Zero Second" in the Unix Epoch is midnight of January 1, 1970 in UTC, therefore it's a few hours earlier in the USA, so UTC shows January as the moment of the big bang, and locations west of Greenwich show December.
The reason your example said January and mine said December goes to the issue of the default timezone settings. The return from failing strtotime() is FALSE, which is loosely interpreted to be zero. The "Zero Second" in the Unix Epoch is midnight of January 1, 1970 in UTC, therefore it's a few hours earlier in the USA, so UTC shows January as the moment of the big bang, and locations west of Greenwich show December.
Open in new window
You didn't ask, but maybe it will save you some time... in JavaScript they number the months from zero, instead of from one. No idea why.
Bookmark these articles, too.
https://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL-Procedural-Version.html
https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html