Update only month and date of string in MySQL/PHP - php

I am trying to update only the month and the day of a string. So example given: In my database i have a DATETIME column. Let's say that has the value 2018-01-08 00:00:00. The user gives in another date which he would like to update. E.g. 2017-11-07 00:00:00. I have a check on the code which descides if the date is different. So we'll skip that. I only want to update the month and the day of that string. So i don't want to update the time and year. The name of my DATETIME column is upDatum I've searched everywhere for this but there is no concrete answer. Here is what i have:
$query = "UPDATE twh_uren_prognose SET upUren =
".$key['inputupuren'].", MONTH(upDatum) = ".$month.", DAY(upDatum) =
".$day." WHERE upID = ".$key['inputupid'];
My question is: How can i only UPDATE the month and the day of a DATETIME MySQL column. I'm a little stuck. The Query which i'm talking about goes like this:
I hope this is kinda clear! Thanks!

You can try making a new date, using the day and month you entered in php and the year you allready had in mysql
$extraWhereTwee = ', MONTH(upDatum) = '.$month.', DAY(upDatum) =
'.$day.'';
$query = "UPDATE twh_uren_prognose SET upUren =
".$key['inputupuren'].", upDatum = STR_TO_DATE('".$day.",".$month.", YEAR(upDatum)','%d,%m,%Y') WHERE upID = ".$key['inputupid'];

Related

Insert two date variables into startDate and endDate fields in the table

I want to insert 2 date variables into startDate and endDate fields in the table.
First is $today, next is $today plus $duration.
I have this piece of code which used to work before, but now it does not work
again.
require_once('connection/config.php');
$today = date("y:m:d"); //get today's date
$duration = 14; //get duration
$sql_insert = mysql_query("INSERT INTO adverts (cust_id, startDate, endDate)VALUES('".$today."', (CURDATE()- WeekDay(CURDATE()) +'".$duration."')");
if($sql_insert){echo 'Successful';}else {echo 'Failed';}
This code worked previously. But now, it's not working again.
It does the insert and returns the result for the endDate as '0000-00-00'.
I do not know why it is returning '0000-00-00'.
Please help me.
Thank you all.
You can't just subtract integers from dates in MySQL (that's an Oracle thing).
You need CURDATE() - INTERVAL WEEKDAY(CURDATE()) DAY to do what you're trying to do.

Retrieve only today records from MySQL database using PHP

In my database I stored date and time as a TIMESTAMP values. I want to retrive only today data from the database. This is code that I tried
$today = date("Y-m-d") . '00:00:00';
$last = date("Y-m-d") . '23:59:59';
$sql = "SELECT id, name, name2,some,some,submittimestamp,some FROM recs where submittimestamp Between $today AND $last";
$result = $conn->query($sql);
But I only get the 0 results message using this. There is more than 100 rows added today.
How can I solve this ? I cant change the database now. only way to solve this is change the PHP script
Do I need to convert PHP datetime to MYSQL timestamp ?
This is my sample database entry time-stamp value
2014-10-02 15:47:01
I only want to retrieve data for one day. time is not required !!
SELECT id, name, name2, submittimestamp
FROM recs WHERE submittimestamp > DATE_SUB(CURDATE(), INTERVAL 1 DAY);
Also you may try this, as DATE() ignores time part
SELECT id, name, name2, submittimestamp
FROM recs WHERE DATE(submittimestamp) = CURDATE();

Using Date functions to pull records from database

I'm truly stumped on something - I have a table in my database with a column called 'today' that has Date and Time records. The column has entries that look like this:
October 25, 2014, 4:58 am
October 25, 2014, 4:36 am
I'm having trouble pulling the rows by date; I think the time stamp is messing with the MySQL query. And I need an SQL query to pull any records where the variable $today matches the date information in the column 'today'. This doesn't work:
$today = date("F j, Y"); // looks like this: October 25, 2014
$result = mysqli_query($link,"SELECT * FROM records WHERE today = $today"); // 'today' represents the column in the table
while($row = mysqli_fetch_array($result)) {
echo var_dump($row);
}
I just get an empty result, I think due to the time stamp. Can someone advise on a better MySQL query that will only grab the rows where $today matches the date in the 'today' column?
Although storing the date and time as string in varchar is not really a good idea, you could still alter your query to match string containing the current date with a LIKE statement:
$result = mysqli_query($link,"SELECT * FROM records WHERE today LIKE '$today%'");
That is just to get your current setup working as a temporary fix but i highly suggest you take a look at datetime and timestamp or similar date types if this is a serious project and not just playing around. with programming.
UPDATE
With a datetime you could get the dates which are the same as today with:
SELECT * FROM `records` WHERE `today` = CURDATE();
with a timestamp you would need to pass it as date so your query would be:
SELECT * FROM `records` WHERE date(`today`) = CURDATE();
You can just use the MySQL date functions:
SELECT *
FROM records
WHERE today = CURRENT_DATE;
If there is a time component on the today column, then the best structure is:
SELECT *
FROM records
WHERE today >= CURRENT_DATE and today < date_add(CURRENT_DATE, interval 1 day)
It's obvious that both dates are not equal. Both dates are treated like text values and are not equal. You need to convert the column containing date in your MySQL query as such:
$result = mysqli_query($link,"SELECT * FROM records WHERE DATE_FORMAT(today, '%F %j, %Y') = $today");
Note that you have to change your column to store values of the type of DATE. Or just use queries as proposed in other answers.

PHP - MySQL query - counting results where timestamp is within specific date ("Y-m-d")

This is pretty basic MySQL, but I have not been able to figure this one out, how to do it correctly..
Example:
I have a DB table named "table1" with a list of records of user visitors data.
Columns:
"ID", "TM" and "IP"
"TM" contains timestamp for when the record is stored.
I have a PHP code where I loop through days from a start date to current day. Like this example:
// Start date
$startdateforarray = '2010-07-21';
// End date
$end_date = date("Y-m-d");
while (strtotime($startdateforarray) <= strtotime($end_date)) {
$timestamp = strtotime($startdateforarray);
//Here I want to run my MySQL Query...
$startdateforarray = date ("Y-m-d", strtotime("+1 day", strtotime($startdateforarray)));
}
Now, inside the loop I want to make a query to count how many results there are in "table1" for each day.
So the MySQL query should be something like:
"SELECT * FROM table1 WHERE TM = (day of $timestamp)"
Of course (day of $timestamp) is where I have a problem.
I know that this should be pretty simple to do, but I havent found a solution yet..
Assuming by timestamp you mean Unix Timestamp, you can do
SELECT * FROM table1 WHERE FROM_UNIXTIME(TM,'%Y-%m-%d') = '2010-07-21'

getting first 5 digits of a database stored number in the query

I'm working with a table and there is field in my table which stores raw time() function value as date.
I want to get rows with today date from this table .
So i figure out when time() func returns a 10 digit number like 1316352184 the first 5 digits are for year , month , day which i need for getting today's date and the rest is for hour minute Second which i dont need
So i get today without hour and... like
$t = time();
$t = $t /100000;
$today =(int)$t;
Now i need to get rows with today date from the table but i'm not sure how to do that.
How can i get first 5 digits of stored date in database in my query to compare it with $date?
Something like this:
$sql = "SELECT * FROM TABLE WHERE ((int)date/100000) as date = $today ;
select * from table
where from_unixtime(unix_timestamp_field,'%Y-%m-%d') = curdate()
Why you don't use:
$sql = "SELECT * FROM TABLE WHERE date(date) = date(NOW());
What you have is a UNIX timestamp. The number of seconds since January 1st, 1970.
You can use date() and mktime() to work out what todays timestamp is, then do date > the timestamp. If that make sense.
Sounds like you should use the DATETIME or TIMESTAMP data type for your column so you can use MySQL's date functions.

Categories