Check if datetime is older than 5 days [duplicate] - php

This question already has answers here:
SELECT all records that are 30 days old
(4 answers)
Closed 1 year ago.
So I am going to make a cron job in cPanel that runs every day, but not sure how the query should look like. I have a datetime column with this type of value: 2021-04-06 14:12:06. How can select from table where datetime column is older than 5 days?

You can use date arithmetic. Assuming you mean "day" without the current time value:
where datetime_col < curdate() - interval 5 day
If you want the reference to be the current time rather than the current date:
where datetime_col < now() - interval 5 day

Related

Laravel: DATE_ADD in where clause [duplicate]

This question already has answers here:
Is there a way to subtract an amount of days from a date in SQL?
(4 answers)
Closed 7 months ago.
I want to subtract days from the db column and then compare it with the current date using DATE_ADD function. Is this possible?
$valid_status_query->whereRaw('DATE_ADD("d",-3, "date_to")', '<=', Carbon::now()->format('Y-m-d'));
For example, if the column date is 2022-07-20 then it should use 2022-07-17 to compare in where clause at the end.
Use it like this:
$valid_status_query->whereRaw("DATE_SUB(date_to, INTERVAL 3 DAY) <= ?", [Carbon::now()->toDateString()]);

Return current date + 7 days in timestamp form [duplicate]

This question already has answers here:
Return current date plus 7 days
(10 answers)
Closed 2 years ago.
So I have the below property where I'm passing in a time()-86000 value, but what would be the best call to generate a timestamp from the time the class method executes and then adds 7 days to that.
Here is what I got:
$profile->set_picture_expiration(time()-86000)
Would using time()-86000 be the right call? I'd like to write it to the DB in timestamp format from the current time + 7 days.
First of all, if you need +7 days, why are you using minus?
Next, 7 days are 7*24*60*60 = 604800 seconds, not 86000.
Finally, the easiest way to get the timestamp for such relative dates is using the strtotime function. In your specific case it would be strtotime('+7 days').
$profile->set_picture_expiration(strtotime('+7 days'));

How to add 30 mins to current timestamp? [duplicate]

This question already has answers here:
adding 30 minutes to datetime php/mysql
(6 answers)
PHP Adding 15 minutes to Time value
(9 answers)
Closed 2 years ago.
So basically I need to add 30mins to the current time and send it as a time stamp in database.
date_default_timezone_set('Asia/Hong_Kong');
$today = date("H:i:s");
I.E i need current time : 10:00 sent time to database 10:30.
I know the codes in sending it to the database i just want to know how to add certain mins or seconds to the current time.
You would use the database time for this:
now() + interval 30 minute
You could set a column with the value as:
update t
set col = now() + interval 30 minute
where id = 1;
Pretty simple, but go with the database solution if possible:
$today = date("H:i:s", strtotime(“+30 minutes”);

How to get exact date from 30 days from specific date? [duplicate]

This question already has answers here:
Add 2 hours to current time in MySQL?
(5 answers)
Add number of days to a date
(20 answers)
Closed 9 years ago.
I have mysql date like this 2013-10-16 17:44:13 and I need to get +30 days from there. I can't use eg. 10th ( month ) and change it to 11th ( month ) as this may be 31 day or 29 depending on the month
I can only think of converting 2013-10-16 17:44:13 to timestamp than + 30*24*60*30, and than this new timestamp back to mysql format
Is there a better way?
You can use strtotime for this:
$date = date('Y-m-d H:i:s', strtotime($date . ' +30 days'));
or do it directly in MySQL using DATE_ADD:
SELECT DATE_ADD(`date`, INTERVAL 30 DAY) as `date` FROM `table`
If you run a newer version of MySQL, you don't need to use DATE_ADD:
SELECT (`date` + INTERVAL 30 DAY) as `date` FROM `table`
Please note that while strtotime is smart enough, MySQL requires you to use DAY. Not DAYS.
Edit: I am unable to find any proof of DATE_ADD being needed in older versions, but I swear that I've heard it somewhere. Take it with a grain of salt and use whatever method you prefer.
since you mentioned mysql you an do it with mysql functions
select NOW() + interval 30 day as NEW_DATE
NOW, could be replaced with a date in your db
select date_field + interval 30 day as NEW_DATE from YOUR_DB

Time Difference [duplicate]

This question already has answers here:
Timestamp Difference In Hours for PostgreSQL
(9 answers)
Closed 8 years ago.
I am new to PostgresQL and PHP and am working with a PostgresQL Timestamp object and am trying to find the difference between now and that timestamp to display in terms of years, months, and days. Is there to do this?
Thanks!
You can just subtract two timestamps, the result is an interval.
To get "now" you can use now() or current_timestamp (among others).
SELECT now() - '2010-02-21 20:11:32';
This will display something like this, though:
830 days 23:00:50.127241
To get a justified representation, use age() or justify_interval()
SELECT justify_interval(now() - '2010-02-21 20:11:32');
Displays the same value in a format like you seem to be after:
2 years 3 mons 20 days 23:01:34.095813
If you want a particular output format use to_char()
select age(now(), '2010-01-02 12:34:35');
The complete version:
select substring(a from 1 for (position('days' in a) + 3))
from (select (age(now(), '2010-01-02 12:34:35'))::text) s(a)
;
substring
------------------------
2 years 4 mons 30 days
(1 row)

Categories