Changing MySQL Table based on Date Using PHP - php

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.

Related

How many days since sign up in mysql with new column

I'd like to add a column to my database which displays the amount of time in days since a user signed up.
Currently I have a field which displays the date they signed up in unix.
Is it possible for the new column to increase its fields by 1 each day?
You can basic SQL to get this information for you dynamically. DATEDIFF() will be what you need:
SELECT
DATEDIFF(CURRENT_DATE, FROM_UNIXTIME(date_signed_up)) AS days_since_signup
FROM
tablename
current time - timestamp_of_registration:
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff
SELECT DATEDIFF(CURTIME(), SIGNUP_DATE);

PHP and MySQL - display contnet of column between certain numerical values

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

Month automatically increasing in phpmyadmin database table

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.

Retrieving a specific row depending on a date variable?

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];

Creating recurring calendar events in PHP/MySQL

I am trying to create an event calendar with recurring events (ex. weekly or monthly) but I cannot wrap my head around it. Can anyone give me some pointers? What is the best way to go about doing this? Any help is greatly appreciated. Thanks.
Create three tables with a structure like:
event table
-id
-schedule_type
-schedule_id
-title
etc.
schedule table
-id
-event_id
-datetime
schedule_recurring table
-id
-event_id
-date
-time
In your event table, the schedule_type field will be either 0 or 1, which would indicate to the application which table the scheduling information is stored in, as well as how to interpret that information.
A non-recurring event will be stored in schedule with a datetime: 2011-09-06 00:00:00, and recurring events will be stored in schedule_recurring with a date: 'every 1st Monday' and a time: 09:30,12:20 (If the event occurs twice on every first Monday of the month).
Maybe this will help get you started!
I know this is an old post but I was thinking the same thing too.
I like the persons solution about using multiple tables to do it, but in fact it can all be done from one table.
Create a table with the following data...
event_title
event_text
event_image
and_other_fields_about_event
recur_code (text)
recur_mode (integer)
start_date (date)
end_date (date)
recur_end_date (date)
recur_mode can have three states -
0 = no recurrence
1 = recurrence with end date
2 = ongoing with no end date (e.g. if you want to add something like 1st Jan as New Years Day)
recur_code would store either NULL or a code in it if the date recurs. The code that should be used there would be the PHP DateInterval code (i.e. P1Y for 1 year or P3M (3 months) or P7D (7 days), etc) - if you want to shrink the data a bit you could chop off the initial 'P' and add it back later as the initial 'P' is always going to be P, it stands for Period so "Period 3 Months" "Period 7 Days", etc.
Then when your retrieving data from the database - you retrieve all data with the following searches
( end_date >= CURDATE () ) OR ( ( recur_mode = 1 ) AND ( recur_end_date >= CURDATE () ) ) OR ( recur_mode = 2 )
(please note this isn't proper SQL - it's just a basic example of the or statement you'd need)
then once you've retrieved the data use PHP with a while loop and DateInterval to increase the start_date until you get to the next re-occurrence also making sure that if recur_mode is set to 1 the start date is not after the recur_end_date.
All done in one table - and also if you want an input form to put the code in then use a hidden field with the dateinterval value in whilst using various radio buttons to select the interval - then use jQuery onchange to update the hidden value with the new selector values.

Categories