Been a long while since I posted here, I have done a LOT of coding without having to ask but I am stuck :(
I have a variable which is called "$date", it is always the current date. Now I have logs which have a date set to them, now what I want to do is delete any logs that are older than a week from the current date. Here's how I'm doing it (? means I don't know what to put).
$date = date('Y-m-d H:i:s');
$one_week_old = $date - ?;
$clearlog = mysqli_query($con,"DELETE FROM logs WHERE logs.date < '$one_week_old'");
Instead of using another variable, you can just query it directly:
$clearlog = mysqli_query($con,"DELETE FROM logs WHERE logs.date <
dateadd(week,-1,getdate()));
You can do this:
$one_week_old = date("Y-m-d",strtotime("-1 week"));
$one_week_old = date('d-m-Y', strtotime("-1 week"));
$date = date('Y-m-d');
$newdate = strtotime ( '-1 week' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-d' , $newdate );
$query="DELETE FROM logs WHERE logs.date < '".$newdate." 00:00:00 '";
$clearlog = mysqli_query($con,$query);
If you don't need this date information later in PHP, i suggest you to do that directly in the MySql Query. Please consider, that this example will modify just the datepart, not the timepart of the current timestamp. It was't specified exactly if you like to to have whole days deleted or exactly the those data older then 604800 seconds (7 x 24 x 60 x 60 = 604800).
MySQL 5.5 Reference Manual 12.7 Date and Time Functions
mysqli_query($con, "DELETE FROM logs WHERE logs.date < DATE_SUB(NOW(), INTERVAL 1 WEEK)");
If you prefer Oriented Object paradigm, we have:
$oneWeekAgo = (new DateTime('-1 week'))->format('d/m/Y');
Explanation:
Return a new DateTimeObject
Set the timestamp from -1 week
Format to your need.
Related
So I have a database entry that update the date/time in yyyy-mm-dd hh:mm:ss.
Now I want to check, if there is a space inbetween the the database and the actual time from 60 minutes. How can I do that?
Example:
DB: 2020-02-14 10:00:00
Time now: 2020-02-14 11:01:00
Do something
DB: 2020-02-14 10:00:00
Time now: 2020-02-14 10:59:00
Do nothing
You can use something like this:
$t1 = strtotime( '2006-04-14 11:30:00' );
$t2 = strtotime( '2006-04-12 12:30:00' );
$diff = round(($t1 - $t2) / 3600);
In MySQL, you can do date arithmetics:
update mytable
set mydatetime = now()
where mydatetime <= now() - interval 1 hour and id = ?
The where clause filters on record whose column mydatetimeis more than one hour old. You did not tell what you want to do , so I assumed an update query, that resets the date/time to the current date/time.
This assumes that you want to update the timestamp of a given record, not accross the whole table, hence condition id = ?, which you can adapt - or remove - for your use case.
Hello Schmaniel at first i think you should use Carbon()
https://carbon.nesbot.com/docs/ to get the right results. It's a great way to work with timeformats and timestamps.
$now = Carbon::now();
$dbtime = Carbon::createFromFormat('Y-m-d H:i:s', '1975-05-21 22:10:22');
$totalDuration = $now->diffForHumans($dbtime);
dd($totalDuration);
$currentDate = new DateTime();
$databaseDate = new DateTime($dateFromDatabase);
$interval = $datetime1->diff($datetime2);
Then you can check using the $interval variable
You can use mysql TIMESTAMPDIFF like this:
SELECT TIMESTAMPDIFF(HOUR,'2003-02-01','2003-05-01 12:05:55');
you can use one of the follwing units:
MICROSECOND (microseconds), SECOND, MINUTE, HOUR, DAY, WEEK, MONTH,
QUARTER, or YEAR.
Say, I have 4 dates and the date today is 1/7/16:
1/7/16 1:06:02
1/7/16 8:01:24
1/8/16 7:02:23
1/6/16 3:12:34
How can I only pick 1/7/16 1:06:02 and 1/7/16 8:01:24.
What date function from PHP can I use to only get the date of today?
Thanks!
UPDATE:
This is being used as a MYSQL selector.
Example: $db->query("DELETE * FROM entry WHERE date='$today'");
How would this work? How can I get my MYSQL query to select only the dates for today?
UPDATE 2:
I've tried using curdate(), but the code is not working...
This is what I'm doing:
$db->query("DELETE * FROM entry WHERE date=curdate()");
What am I doing wrong?
LAST UPDATE: curdate() worked properly...
UPDATE: As MySQL query, you can do this as follows:
$db->query("DELETE * FROM entry WHERE DATE(date) = CURDATE()");
CURDATE() returns today's date.
If you want to do with php, see below to use the DateTime class.
$now = new DateTime;
$otherDate = new DateTime('2016-01-01'); // or e.g. 2016-01-01 21:00:02
// Setting the time to 0 will ensure the difference is measured only in days
$now->setTime( 0, 0, 0 );
$otherDate->setTime( 0, 0, 0 );
var_dump($now->diff($otherDate)->days === 0); // Today
var_dump($now->diff($otherDate)->days === -1); // Yesterday
var_dump($now->diff($otherDate)->days === 1); // Tomorrow
The answer by schellingerht is totally correct, but
$now = new DateTime;
should be this instead:
$now = new DateTime('Today');
That will ensure that the date from today with 0 hours and 0 minutes is selected. Otherwise the output could be Today even if the date is tomorrow.
Hope that helps!
Convert to timestamp and then format for only the date. Then compare to the same format of todays date:
var_dump(
date('Ymd', strtotime('1/7/16 1:06:02')) === date('Ymd')
);
Try using DATE() (http://www.w3schools.com/sql/func_date.asp). You don't need to convert your dates to a string - this is not efficient.
DELETE FROM yourTable WHERE DATE(yourDateField) = DATE(NOW)
You could compare today's date with Y-m-d format, with your date parsed with the same format.
$today = date('Y-m-d');
$otherdate = date('Y-m-d', strtotime('2022-02-02 16:00:26'));
if($today === $otherdate){
\\ your code for Today
} else {
\\ your code for not Today
}
I'm trying to count the rows with a datetime less that 10 minutes ago but the current time its being compared to seems to be 1 hour ahead so Imm getting 0 results, if I go into my table and put some fields forward an hour then I get results.
Getting results:
$stmt = $db->query('SELECT check_log FROM members WHERE check_log >= DATE_SUB(NOW(), INTERVAL 10 MINUTE)');
$row_count = $stmt->rowCount();
echo $row_count.' Members online.';
The datetime of the field of of typing this is 2013-07-11 16:54:12 and I'm getting no results but if I manually change the date time to 2013-07-11 17:54:12 I get 1 result the datetime was input seconds ago using:
$date = date("Y-m-d H:i:s");
The 17:54:12 is my local time and 16:54:12 seem to be my servers time, is my compare trying to look into the future or is it using my local time as a reference?
PHP and MySQL don't agree on the current timezone.
Pass the desired time in as a literal from PHP to SQL instead of using NOW().
Always store date times in php's timezone.
One function you can particularly make use of is strtotime.
$now = strtotime("now"); // current timestamp
$hour_later = strtotime("+1 hour"); // hour later
$now = date("Y-m-d H:i:s", $now);
$hour_later = date("Y-m-d H:i:s", $hour_later);
I have a table with a date field type date.
What I am trying to do is to do a comparison between the date from inside the table and the today date. If the date from the table is yesterday then insert the today date.
The thing I'm not sure about is how to insert the data in the database so I can make the comparison. here is what im thinking to do"
$d = time();
$x = mysql_querry("SELECT date FROM table where id = $id", $con);
while($y = myqsl_fetch_array($x)){
$oldTime = $y['date'];
}
if ($oldTime < $d){
$i = mysql_querry("INSERT INTO table (date) VALUES (CURDATE()) ", $con);
}
So, I'm not sure if $oldTime and $d can be compared like that, but I hope you guys get my point.
Any ideas?
Thanks
You can't do in that way because the CURDATE() function return a date in a format like 2011-11-11 while time() returns the number of seconds since the January 1 1970 00:00:00 GMT.
Anyway you can change the format of the time() to look like the CURDATE() using the date() function in this way:
date('Y-m-d', time())
or even better, to get the current date, you can use just this line:
date('Y-m-d')
To conclude, you can do the if in this way:
if( strtotime($oldTime) < strtotime(date('Y-m-d')) )
or even better:
if( strtotime($oldTime) < strtotime('now') )
To compare dates you can use strtotime($date); Where date can be a time(), mysql date or date('Y-m-d'); string
I was looking at this post, and it is close to what I need:
PHP - How to count 60 days from the add date
However, in that post, the calculation is performed by adding 60 days to the current date. What I need to do is calculate the date based on a variable date (and not the current date).
Something like this:
$my_date = $some_row_from_a_database;
$date_plus_10_days = ???;
Anyone know how to do that?
Thanks
You can put something before the "+10 days" part:
strtotime("2010-01-01 +10 days");
Use date_add
http://www.php.net/manual/en/datetime.add.php
$my_date = new DateTime($some_row_from_a_database);
$date_plus_10_days = date_add($my_date, new DateInterval('P10D'));
You will have to look into strtotime(). I'd imagine your final code would look something like this:
$dateVariable = strtotime('2017-01-29');//your date variable goes here
$date_plus_60_days = date('Y-m-d', strtotime('+ 60 days', $dateVariable));
echo $date_plus_60_days;
If you are using PHP >= 5.2 I strongly suggest you use the new DateTime object. For example like below:
$date_plus_60_days = new DateTime("2006-12-12");
$date_plus_60_days->modify("+60 days");
echo $date_plus_60_days->format("Y-m-d");
I see you are retriving data from a database.
If you are using mysql you can do it on the select:
Example: you need the last date of the table and this date-7 days
select max(datefield) as ultimaf, DATE_SUB(max(datefield),INTERVAL 7 DAY) as last7
from table
It´s easy use curdate() if you want todays date.
If you need a dynamic between that selects the count of last 7 days:
select count(*) from table
where DATE_SUB(CURDATE(),INTERVAL 7 DAY)<=datefield"
date('Y-m-d H:i:s', strtotime("2014-11-24 06:33:39" +35 days"))
this will get the calculated date in defined format.
Suppose today's date is
date_default_timezone_set('Asia/Calcutta');
$today=date("Y-m-d");
And i can add 10 days in current date as follows :
$date_afte_10_days = date('Y-m-d', strtotime("$today +10 days"));