I have 7 columns that which contain the information of closing times, each for one day. (It goes like VENUE_CLOSE_T_MO, VENUE_CLOSE_T_TU... etc)
How would I, for example choose one of those columns depending on a date variable ($somevariable) which contains a specific date?
For example, if the date variable was Sunday, March 18 22:00, it would choose column VENUE_CLOSE_T_SU.
Thanks for the help everyone!
EDIT (Solution given by TEEZ that solved the issue)
My Date variable is $Start.
And this is the code:
$day_name=strtoupper(date('D',$start));
$day_name=substr($day_name,0,2);
$selectcolumn='VENUE_CLOSE_T_'.$day_name;
So in this case $selectcolumn = VENUE_CLOSE_T_SU
And the echo is then this:
$row[$selectcolumn]
Thanks for all your help again Teez!
first get day name from variable ($somevariable)
$day_name=strtoupper(date('D',$somevariable));
then make query like below for getting column according to day in $somevariable
select concat('VENUE_CLOSE_T_',left($day_name,2)) as datecolumnname from tableame
EDIT:
OR
you don't need to do this in query if you taking all column in query. just add these lines in php code where you printing data in we page under date column
$day_name=strtoupper(date('D',$somevariable));
$day_name=substr($day_name,0,2);
$selectcolumn='venues.VENUE_CLOSE_T_'.$day_name;
echo $row[$selectcolumn];
Related
Is It Possible To Change MySQL Table Column Based On Date Using PHP
For Example
I Want To Change yearsold = 19 to yearsold = 20 After 1 Year
So When It Reach 2016 yearsold become 20
Thanks :D
EDIT: I Need It To Work Automatically not Having To Visit The Page Over and Over
You could do 1 of 2 things. First would be save the birthdate and calculate it every time you load the object.
The second would be to create a view that calculates the age as a column in the view, so that every time you select from the view the age is "updated" and displayed correctly. You can check out the documentation for mysql views here.
So here is the thing I get dates from users from select tags like this:
<select>
<?php
for($i=date('m'); $i>=0; $i--){
echo "<option>".$i."</option>";
}
?>
</select>
Now what this does is give 05 as first option 4,3,2,1 as next options, I want 04,03,02,01 as all options.
How can I do that?
Also I am running a sql query that deletes rows by matching dates.
So the date selected by user comes as 2013-5-18(without zero) but the date in database is 2013-05-18 (with zero), so the dates don't match and the query is unable to delete the row.
Also can I change the way date is stored in a database?
I store date as 2013-5-18(without zero) and it automatically gets stores as 2013-05-18 (with zero).
Any other thing that will just help me to match the dates so I can run the delete query?
Try:
echo "< option>".sprintf("%02d", $i)."< /option>";
sprintf will format your number to have a preceding 0 when required.
What you're seeing is date('m') properly returning the value 05 for the first month. However when you add 1 to it ($i++) it's being cast to an integer, so the next value is 6. I might presume your actual code uses $i--, as this presently looks a lot like an infinite loop.
You might also consider handling the formatting when you build the query, rather than on form presentation. $month = sprintf("%02d", $_POST['month']); rather than relying on the client to pass it forward nicely.
I'm new to MySQL and PHP but was wondering if someone could help me with a little project I'm doing for my boss.
I have a SQL database (MyDB) and a table in there (mytable) with two columns - the first column (index) is an auto-incrementing integer from 1-10, the second column (date) has different dates and timestamps in the format of Year-month-day time 2013-04-12 1326
I'm trying to create a simple PHP page that first gets the current date (easy enough) then looks at the table and shows the number of rows that fall within yesterday's date. For example, if I have 3 rows with 2013-04-11 XXXX and 2 rows with 2013-04-12 XXXX (and today is the 12th April 2013) the page will display 3. (The time is not important but we can't remove it from the table as it's auto created by one of the other staff's programs and he refuses to change it).
So far I've got my php page, done a connection to the DB and defined two variables:
$startdate = date('Y'."-".'n'."-".'d'." "."0000");
$enddate = date('Y'."-".'n'."-".'d'." "."2359");
As the timestamp doesn't matter I've gone for the min/max possible on the variables. I realise this will only give the current date, trying to work out how to get it to display the previous day as the date in the variable.
Now I'm trying to create a sql query that will count the number of rows where the date field falls within the startdate and enddate variables (-1 day) but not too sure where to start or how this would look. I then need to output this as a variable in PHP so I can echo it later in the page.
Anyone able to point me in the right direction? Hope any of this makes sense.
You could write a query with no params to do this (if its always just yesterday).
SELECT * FROM <table>
WHERE DATE_FORMAT(<date column>,'%j-%Y') = DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 DAY), '%j-%Y');
Date functions in the where clause might not be super awesome performance wise
I'm running into a weird issue where I'm posting a date in Y-m-d format yet it's being changed to a completely different date once I view in the actual MySQL table.
Here's the query
UPDATE $admins_table
SET expire=$expireu
WHERE identity='$donation_row[steam_id]
The expire field is what I'm having issues with. The field itself is a varchar, and the $expireu variable is always a date in Y-m-d format ex. 2013-11-16
When that query is run, with the date I gave as an example above, I get a weird result in the actual MySQL table. If I go to view the table, instead of it storing 2013-11-16 it has stored 1986 as the date. No month or day, just 1986.
I may have made a very stupid/silly mistake, but at this point I'm unsure of what I've flubbed. Any help in the right direction would be much appreciated, thank you.
haha, use quotes!
UPDATE $admins_table SET expire='$expireu' WHERE identity='$donation_row[steam_id]'
mysql substracts 2013-11-16 == 1986
the use of ' and " are your friends. you are passing a math problem into mysql which it is solving and then saving the result of. wrap that date in quotes.
Is there any way to increase a table field named month every month..or I should say to the latest month...Just need to update the month value..i.e. In, it should be 1
march 3
July 7
etc.
Help me.
Closest you'll get without manually inserting idate('m') with every new record and without using fancy mysql... is by using mysql default TIMESTAMP field. Check this out:
http://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html
But this a date so you'll need to use MONTH(Field) to get the month.