How to clear that cache of current data?
$result = Customer::getDb()->cache(function ($db) use ($id) {
return Customer::findOne($id);
}, 60 * 60 * 24 * 4);
I want to clear the cache of current data in Customer after updating
You can modify this code to use data cache instead of query cache so you can use unique key.
$data = $cache->get('customer' . $id);
if ($data === false) {
$data = Customer::findOne($id);
$cache->set('customer' . $id, $data, 60 * 60 * 24 * 4);
}
or starting from 2.0.11:
$data = $cache->getOrSet('customer' . $id, function () use ($id) {
return Customer::findOne($id);
}, 60 * 60 * 24 * 4);
So now you can use
$cache->delete('customer' . $id);
you can use flush for global.
Yii::$app->cache->flush();
you can use TagDependency :
$result = Customer::getDb()->cache(function ($db) use ($id) {
return Customer::findOne($id);
}, 60 * 60 * 24 * 4, new TagDependency(['tags'=>'customer']));
//to flush
TagDependency::invalidate(Yii::$app->cache, 'customer');
For more information check here
Related
Is there any function that will give me the name of passed time with two dates in mysql? for example, after i've wrote a post then the page redirects me to the post page it must say " posted 4 seconds before", and afte five minutes later it will say posted 5 minutes before". If the time bigger than 60 minutes it will say xx hours before ...
15 days later it must say 2 weeks ago.
Something like wordpress meta info.
Same as time on my nick name at to right bottom of this post
I suggest you to write this on scripting side. If you are using PHP you can use the following code block to convert unixtime to relative phrase.
function relative_date($date)
{
$diff = time() - $date;
if ($diff < 0) {
return "just now";
} else {
$app = "ago";
}
$t_diff = $diff;
$days = floor($diff / 3600 / 24);
$diff = $diff - ( $days * 3600 * 24 );
$hours = floor($diff / 3600);
$diff = $diff - ( $hours * 3600 );
$minutes = floor($diff / 60);
$diff = $diff - ( $minutes * 60 );
$seconds = $diff;
if ($t_diff > 60 * 60 * 24) {
$str = ( $days . ' days' );
} else if ($t_diff > 60) {
$str = ( $hours ? "{$hours} hours" : "" ) . ( $minutes ? " $minutes minutes" : "" );
} else {
$str = ( $seconds ) . " seconds";
}
$str = $str . " $app";
return $str;
}
This is what I want
function formatDateDiff($start, $end=null) {
if(!($start instanceof DateTime)) {
$start = new DateTime($start);
}
if($end === null) {
$end = new DateTime();
}
if(!($end instanceof DateTime)) {
$end = new DateTime($start);
}
$interval = $end->diff($start);
$doPlural = function($nb,$str){
if ($nb > 1) {
switch ($str) {
case 'Yıl':
case 'Ay':
case 'Gün':
return $str.'e';
case 'Saat':
case 'Dakika':
case 'Saniye':
return $str.'n';
}
} else
return $str;
}; // adds plurals
$format = array();
if($interval->y !== 0) {
$format[] = "%y ".$doPlural($interval->y, "Yıl");
}
if($interval->m !== 0) {
$format[] = "%m ".$doPlural($interval->m, "Ay");
}
if($interval->d !== 0) {
$format[] = "%d ".$doPlural($interval->d, "Gün");
}
if($interval->h !== 0) {
$format[] = "%h ".$doPlural($interval->h, "Saat");
}
if($interval->i !== 0) {
$format[] = "%i ".$doPlural($interval->i, "Dakika");
}
if($interval->s !== 0) {
$format[] = "%s ".$doPlural($interval->s, "Saniye");
}
if(count($format)==0 || (count($format)==1 && $interval->s !== 0)) {
return "Az Önce";
}
// We use the two biggest parts
if(count($format) > 1) {
$format = array_shift($format).", ".array_shift($format);
} else {
$format = array_pop($format);
}
// Prepend 'since ' or whatever you like
return $interval->format($format);
}
Reference Address: https://www.php.net/manual/en/dateinterval.format.php
Thank you so much for your reply.
I need to update month_no fields of the records resulting from a select query using some variables passed from controller but every field is written with the same value.
These are the variables:
$id_calc = 27
$from_y = "2013-05-01"
$to_y = "2015-11-31"
The table result:
id id_paid year_ref month_no
1 26 2012 12
2 26 2013 12
4 27 2013 12 (should be 8)
5 27 2014 12
6 27 2015 12 (should be 11)
This is model:
public function CorrectTable($id_calc, $from_y, $to_y)
{
$connection = Yii::$app->db;
$query = "SELECT * FROM my_table where id_paid='$id_calc'";
$data = $connection->createCommand($query)->queryAll();
// calculate no. of month first and last year
$month_no_begin = (13 - date("m",strtotime($from_y)));
$month_no_end = (date("m",strtotime($to_y)));
//
foreach($data as $row)
{
$id = $row['id'];
// calculate number of month per year
if ($row['year_ref'] = date("Y",strtotime($from_y)))
{
$month_no = $month_no_begin;
} elseif ($row['year_ref'] = date("Y",strtotime($to_y)))
{
$month_no = $month_no_end;
} else
{
$month_no = 12;
}
Yii::$app->db->createCommand()
->update('my_table', ['month_no' => $month_no], ['id' => $id])
->execute();
}
I tried to put the above code in controller with the same result.
A careless mistake once I had made before. You should use == or === in the if statement to test equal condition, instead of =
public function CorrectTable($id_calc, $from_y, $to_y)
{
$connection = Yii::$app->db;
$query = "SELECT * FROM my_table where id_paid='$id_calc'";
$data = $connection->createCommand($query)->queryAll();
// calculate no. of month first and last year
$month_no_begin = (13 - date("m",strtotime($from_y)));
$month_no_end = (date("m",strtotime($to_y)));
//
foreach($data as $row)
{
$id = $row['id'];
// use `==` instead of `=`
if ($row['year_ref'] == date("Y",strtotime($from_y)))
{
$month_no = $month_no_begin;
}
// use `==` instead of `=`
elseif ($row['year_ref'] == date("Y",strtotime($to_y)))
{
$month_no = $month_no_end;
}
else
{
$month_no = 12;
}
Yii::$app->db->createCommand()
->update('my_table', ['month_no' => $month_no], ['id' => $id])
->execute();
}
}
our if ... esleif ... else sequence seems not logig
as you done the else if is never executed
so you should change the condition this way the e
foreach($data as $row)
{
$id = $row['id'];
// assigne default value
$month_no = 12;
// check for month_begin
if ($row['year_ref'] == date("Y",strtotime($from_y)))
{
$month_no = $month_no_begin;
}
//check for month_end
if ($row['year_ref'] == date("Y",strtotime($to_y)))
{
$month_no = $month_no_end;
}
Yii::$app->db->createCommand()
->update('my_table', ['month_no' => $month_no], ['id' => $id])
->execute();
}
I have error on Wordpress. Help please )
PHP Notice: Undefined offset: 0 in /home/userpro/public_html/wp-content/themes/hoon/inc/tweaks.php on line 602
/**
* Display Future Posts
*
* Display future post in the events category to all users.
*/
function hoon_show_all_future_posts( $posts ) {
global $wp_query, $wpdb;
if ( is_single() && $wp_query->post_count == 0 ) {
$events_cat = hoon_option( 'events_category' );
$request = $wpdb->get_results( $wp_query->request );
// below line 602 //
if ( post_is_in_descendant_category( $events_cat, $request[0]->ID ) || in_category( $events_cat, $request[0]->ID ) ) {
$posts = $request;
}
}
return $posts;
}
add_filter( 'the_posts', 'hoon_show_all_future_posts' );
I have to fetch all data from table which has latitudes / longitudes values.Actually I just required all latitudes / longitudes values which is within 1000 miles from a source, So for that I used concept & it's calculating latitudes / longitudes properly but when I am creating a find query to fetch data which has table.latitudes <= latitudes North and table.latitudes >= latitudes South
Similarly for longitudes.
I have made these calculation code & find query
$lat1 = $coodSearch['lat'];
$lon1 = $coodSearch['long'];
$d = $coodSearch['dis'];
$r = 3959;//earth's radius in miles
//compute max and min latitudes / longitudes for search square
$latN = rad2deg(asin(sin(deg2rad($lat1)) * cos($d / $r) + cos(deg2rad($lat1)) * sin($d / $r) * cos(deg2rad(0))));
$latS = rad2deg(asin(sin(deg2rad($lat1)) * cos($d / $r) + cos(deg2rad($lat1)) * sin($d / $r) * cos(deg2rad(180))));
$lonE = rad2deg(deg2rad($lon1) + atan2(sin(deg2rad(90)) * sin($d / $r) * cos(deg2rad($lat1)), cos($d / $r) - sin(deg2rad($lat1)) * sin(deg2rad($latN))));
$lonW = rad2deg(deg2rad($lon1) + atan2(sin(deg2rad(270)) * sin($d / $r) * cos(deg2rad($lat1)), cos($d / $r) - sin(deg2rad($lat1)) * sin(deg2rad($latN))));
$this->loadModel('City');
$finder1 = array( 'and' => array('City.latitude <=' => $latN,'City.latitude >=' => $latS ));
$latArr = $this->City->find('all', array('conditions' => $finder1));
$finder2 = array( 'and' => array('City.longitude >=' => $lonE,'City.longitude <=' => $lonW ));
$longArr = $this->City->find('all', array('conditions' => $finder2));
I have checked Calculated value($latN,$lonE) its giving result but find all query is not working.
Please help me where I am going wrong.
This might do the trick
$data = $this->ModelName->find('all', array('conditions' => array('ModelName.DbColumn >=' => 10)));
$data = $this->ModelName->find('all', array('conditions' => array('ModelName.DbColumn <=' => 10)));
$data = $this->ModelName->find('all', array('conditions' => array('ModelName.DbColumn >=' => 10,'ModelName.DbColumn <=' => 20)));
Ι have a table in MySQL with
date starttime endtime
11/2/2014 10:00:00 10:45:00
11/2/2014 10:45:00 11:15:00
11/2/2014 11:45:00 12:15:00
11/2/2014 12:15:00 13:30:00
I need the sql query to give back to me the time that I can set a new meeting for 30 min
$array_time = array();
$query = "SELECT starttime, endtime FROM table";
$result = $mysqli->query($query);
//fetch result
while ($array_time[] = $result->fetch_assoc()) {}
/*
$array_time = Array (
...
1 => array('starttime' => 10:45:00, 'endtime' => 11:15:00)
2 => array('starttime' => 11:45:00, 'endtime' => 12:15:00)
...
)
*/
//array of free time for your meeting
$free_time = array();
foreach($array_time as $i => $row){
if($i > 0) {
//if this is not the first time row
//get this row starttime
$this_starttime = strtotime($row['starttime']);
//and the last endtime
$prev_endtime = strtotime($array_time[$i-1]['endtime']);
//get the difference
$diff = $this_starttime - $prev_endtime;
//if difference is more or equal than 30 minutes
if($diff >= strtotime('30 minutes')) {
$free_time[] = array('starttime' => $row['starttime'], $array_time[$i-1]['endtime']);
}
}
}
I think you can't do this in MySQL query, so PHP could help you.
Hope it works, I have not tested it out.
But try to save your data by other way, this is not so effective, as you can see.