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

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.

Related

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();

Filter Data before a date and time

I am begginer using PHP and MSQL and I can not filter the results of my table correctly. I want to show me all the fields in the table that keep the condition that there are still two hours for the date and time I have stored.
I used this query but does not work well, because if the current month is 12 and is stored in the DB 1 is not shown, as if even the time stored is less than 2 hours even another day:
putenv('TZ=Europe/Madrid');
$anoE = date("y");
$mesE = date("m");
$diaE = date("d");
$horaE = date("H");
...
$consulta = "SELECT tipo,nombre,descripcion,hora,minuto ,lugar,duracion,fecha,horacero,id,ano,dia,mes,equipo FROM ".$tabla." WHERE ".$anoE." <= `ano` AND ".$mesE." <= `mes` AND ".$diaE." <= `dia` AND ".$horaE." <= `(hora+2)` ORDER BY ano,mes,dia,hora,minuto";
I don't know how i can write the while condition for the query shows fields which remain them two hours beyond the current time (same day)
If your field type is DATETIME (if not, make it) you can use simple query:
SELECT *
FROM table_name
WHERE datetime_field_name BETWEEN DATE_SUB(NOW(), INTERVAL 2 HOUR) AND NOW()
Which will select all records that are between now - 2 hours and now.

MySQL date comparison from PHP

I would very much appreciate your help.
I have a mysql db that contains a Datetime field. In that field I have a particular date and time,
for example:
2013-10-03 22:28
I then have a PHP script that gets a particular date and time from the $_GET command, values separately: year, month, day, hour, minute.
From this GET I created a Date as follows:
$datum = $year."-".$month."-".$day." ".$hour.":".$minute.":".$seconds;
$date = date('Y-m-d H:i',strtotime($datum));
What I need to do now is somehow compare this new date I created with the last date value in the database (the most recent). The point is that after I compare this I want to check whether the last value in the db is older than 5 minutes and only then do a particular action (insert new row), and if the last date in the db is newer than 5 minutes, do nothing.
Use "select max(datetime_field) from tblname" to get the most recent value for the Datetime field.
$now = new DateTime();
$dateFromDB = new DateTime($someValueFromYourDataBase);
// subtract 5 minutes from now and compare with the stored timestamp
if ($now->sub(new DateInterval('PT5i') > $dateFromDB) {
// database timestamp is older - do something
}
you can use SQL like this:
select count(*) from `table` where `datefield` >= '$date'
$date can be calculated like
$date = date('Y-m-d H:i', strtotime($datum) - 5*60);
5*60 - it is 5 minutes
for performance reason I suggest you to add index to datefield and change select like:
select `datefield` from `table` where `datefield` >= '$date' limit 1

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.

Getting any rows which were added before a certain date

Would this be possible? I've used this to insert the date into a field called "date":
$date=date("m/d/y");
$sql="INSERT INTO pool (date) VALUES('$date' )";
$result=mysql_query($sql);
I've used this statement to get the date a week ago:
$variable = date('d-m-y', strtotime('-1 week'));
So how would I SELECT any rows which were added last week?
Instead of storing your dates as m/d/y, you should store them as Y-m-d :
$date=date("Y-m-d");
$sql="INSERT INTO pool (date) VALUES('$date' )";
In the database, you dates will then look like 2011-04-09.
That format is much easier to work with : alphabetical comparisons will work.
Which means that searching for rows that are older than a certain date would become something like this :
$variable = date('Y-m-d', strtotime('-1 week'));
$query = "select * from pool where date < '$variable'";
Also note that instead of working with a date field which is a varchar (or an equivalent) in your database, you could use a DATE column -- which would allow to to work with date and time functions in MySQL.
If the date field is a proper date type you can do < or > in your sql query. For example -
SELECT * FROM table WHERE date > '$date'
If you want everything from 1 week ago to now you can do something like the above or
SELECT * FROM table WHERE date BETWEEN '$date' AND NOW()

Categories