Am trying to perform a query and get items where created_at is not greater that 24 hrs
I have tried
$trucks = Orders::find()
->where(["created_at"=>not more than 24 hrs ]) //stuck here
->orderBy(['created_at' => SORT_DESC])->all();
Nb:Created_at is in unix timestamp.
in usual php it would be the equivalent of
$created_at= 1500373706; // time order was created
if ((time() - $created_at) > 86400) {
//Dont get these
} else {
//Get these
}
How do i go about this?
I found this answer and made some very slight modifications to fit your situation: How to compare Dates from database in Yii2
$yesterday = strtotime("-24 hours");
$trucks = Orders::find()->where(['<=', 'created_at', $yesterday])
->orderBy('created_at DESC')->all();
Related
I'm trying to write a function to calculate the next date that a piece of equipment needs to be checked. I'm using the code below (it's incomplete.)
function get_next_check(){
$today = date(get_option('date_format'));
$first_check = types_get_field_meta_value( 'first_check', $post_id );
// Interval is a number of weeks- ie. month = 4, year = 52
$interval = types_get_field_meta_value( 'interval', $post_id );
// Calculate the next date after today that the check needs to be performed
$next_check = ;
return $next_check;
}
add_shortcode( 'next_check', 'get_next_check' );
I'm guessing I need to create an array of all possible dates, and compare each to today's date, only returning the next one?
Assuming that $first_check is a string with the date in it, and you want the function to return the same, something like this might work:
$week = 60 * 60 * 24 * 7; // number of seconds in a week
$next_check = strtotime($first_check);
while ($next_check < time()) {
$next_check += $week * $interval; // advance the check time by the desired interval
}
return date(get_option('date_format'), $next_check);
You can eliminate your initialization of the $today variable with this.
You can achieve this on the proposed by Greg Schmidt , or alternatively by doing:
// Calculate the next date after today that the check needs to be performed
$next_check = date(get_option('date_format'), strtotime($today." ".$interval." weeks"));
I am building a chart and for that extracting data from the past 24 hours, 48 hours, 1 week and 2 weeks.
I used INTERVAL statements for this purpose but they are not working as expected. I am using Laravel. Here is the function to extract the data:
public function range($range)
{
$data = new Main();
$data0 = $data->whereRaw('updated_at >= DATE_SUB(NOW(), INTERVAL '.$range.')')->whereRaw('MINUTE(updated_at)>54')->orwhereRaw('MINUTE(updated_at)<6')->where('server_short_name', '=', 'FiveRP')->get();
$data1 = $data->whereRaw('updated_at >= DATE_SUB(NOW(), INTERVAL '.$range.')')->whereRaw('MINUTE(updated_at)>54')->orwhereRaw('MINUTE(updated_at)<6')->where('server_short_name', '=', 'GTALife')->get();
$data2 = $data->whereRaw('updated_at >= DATE_SUB(NOW(), INTERVAL '.$range.')')->whereRaw('MINUTE(updated_at)>54')->orwhereRaw('MINUTE(updated_at)<6')->where('server_short_name', '=', 'GermanV')->get();
return compact('data0', 'data1', 'data2');
}
Here is the function that calls the view:
public function hours24()
{
$t = $this::range('24 HOUR');
return view('chart', $t);
}
But in the chart, I am getting results from 22nd of May as well which is the earliest date on DB. Which means that the code isnt working as expected although it is correct according to me. Is there something I am missing out?
I have found the solution. Actually in my code I am using an 'orwhereRaw' which means that I am implying an OR between all the where statements which is sadly not what I want.
I have a mySQL database that stores the checkin and checkout time of a person in a gym. I have imported the checkin and checkout times in to my PHP script. Now I want to deduct the two timestamps from each other - giving me the time left. I want this to display in minutes.
This is my idea:
$checkOut = "2016-01-31 15:01:11";
$checkIn = "2011-01-31 15:32:35";
echo ($checkIn - $checkOut);
// I want this to display 31 minutes.
I have seen many examples on StackOverflow, but none matched my description and I couldn't reverse engineer the ones I found - because they use the time() function - which I guess takes the current time.
You can use strtotime();
$checkOut = "2016-01-31 15:01:11"; // initial value
$checkIn = "2011-01-31 15:32:35"; // initial value
$checkOut_stamp = strtotime($checkOut); // Converting to unix timestamp
$checkIn_stamp = strtotime($checkIn); // Converting to unix timestamp
echo date("i", ($checkIn - $checkOut)) . 'Minute(s)';
IMP Note: The above method will only work if the minutes are below 59, or else the hours will be rounded off and discarded. So if your requirements is showing the time in minutes which can be grater than 59 minutes eg. 144 minutes, then you'd want to just divide by 60, as follows.
$checkOut = "2016-01-31 15:01:11"; // initial value
$checkIn = "2011-01-31 15:32:35"; // initial value
$checkOut_stamp = strtotime($checkOut); // Converting to unix timestamp
$checkIn_stamp = strtotime($checkIn); // Converting to unix timestamp
$seconds = $checkOut_stamp - $checkIn_stamp;
if($seconds > 0){
if($seconds > 60){
$minutes = $seconds/60;
} else {
$minutes = 0;
}
} else {
$minutes = 0;
}
echo $minutes . ' Minute(s)';
$checkOut = "2016-01-31 15:01:11";
$checkIn = "2011-01-31 15:32:35";
$time = (strtotime($checkIn) - strtotime($checkOut));
echo date('i',$time);
use this code
If you were fetching this record from database then you can simply achieve it using MySql function TIMESTAMPDIFF, as no need to use PHP function over here
Select TIMESTAMPDIFF(MINUTE,checkIn,checkout) as tot_diff from your_table
I need to get the days left before the date is reached, and if the timestamp is before the current date like 1990 or something, then it will display a message.
so say it's the 1st november of 2014
if the timestamp is before the first of november, 1st 2014, then it will display expired, else it will tell you how many days left before the date is reached.
thanks you very much.
This is in php by the way.
<?php
$current = time();
$target = '2014-11-01 00:00:00';
$target = strtotime($target);
if($target > $current){
$span = $target - $current;
$span = ceil($span / (60 * 60 * 24));
echo 'You have less than '.$span.' days!';
} else {
echo 'Time has already expired!';
}
The above will output
You have less than 39 days!
What i would do is set a DateTime Class for each unix timestamp you have. Compare the two Objects with DateTime's Diff Function.
$TodaysDate = new DateTime();
$TodaysDate->setTimestamp(time());
$ExperationDate= new DateTime('2014-10-01');
$interval = $TodaysDate->diff($ExperationDate);
If($interval <= 0){
echo 'Product Expired';
}
Their are multiple ways you can set the time in the DateTime Object, using timestamps or keywords or specific date formats.
Just subtract the current time from your timestamp and divide - Unix time is in seconds.
I have to make a notification after 30 days.
foreach ($pdo->query($sql) as $row) {
$date = date_create($row['data']);
$laikotarpas = date_diff(new DateTime("now"), $date);
// var_dump($liko);
$liko = 30 - $laikotarpas->d;
I want correct result in days.
I have added a row at 2014.03.19 and this shows that left 3days to 30.
My goal is to achieve:
I add record at 2014.03.19 and get result how many days have passed from today. I thought that $laikotarpas->d gives a duration in days, but, when i do calculations to set the limit for 30days.
So my main problem is to get correct $liko, but I have no idea how.
I am adding my time using this code (using PDO):
$q->execute(array($name,
date("Y-m-d H:i:s", time())
);
In my database I use DATETIME. And i print that date from SQL using this php:
<?php echo date_format(date_create($data['data']), 'Y-m-d'); ?>
Is my way good? How to improve this?
-----edit-----
I have to use php5.2
Just got an idea, it takes only days and ignores months passed count. How to update that to count duration only in days?
If you want to find the difference between now and a date in the past, try something like this:
PHP >= 5.2.0
$then = '2014-03-19';
$date = new DateTime($then);
$now = new DateTime('now');
$diff = $date->diff($now);
echo $diff->days . ' days since ' . $then . PHP_EOL; // 58 days since 2014-03-19
PHP < 5.2.0
$date = strtotime($then);
$now = time();
$diff = $now - $date;
$days = round($diff / 60 / 60 / 24); // convert seconds to days and round off
Note: after understanding more about your problem, I highly suggest you filter your results based on date ranges in MySQL rather than PHP - it'll be easier and more economic and will reduce your potential risk for affecting data you didn't mean to. See Cull Larson's answer.
You could just use a query like this:
SELECT * FROM myTable WHERE DATE(signupDate) = DATE_SUB(NOW(), INTERVAL 25 DAY);
That will give you all results with a signup date that is 25 days old. If you have a flag in the table telling you whether you've notified them, you can pass that along too:
SELECT * FROM myTable WHERE notified=false AND DATE(signupDate) = DATE_SUB(NOW(), INTERVAL 25 DAY);
If you want to get every record 25 days or older, that hasn't been notified:
SELECT * FROM myTable WHERE notified=false AND DATE(signupDate) <= DATE_SUB(NOW(), INTERVAL 25 DAY);