I need to query the date:- 2020-01-06 If this date ( 2020-01-06 ) exists in the _from field in the post meta table.
_form field is in timestamp format like 1578304800.
This is how I am trying to do.
$theBookingTime = strtotime( '2020-01-06' );
$events_query = new WP_Query(
array(
'post_type' => array('yith_booking', 'post'),
'meta_query' => array(
array( 'key' => '_from',
'value' => date('c', $theBookingTime),
'compare' => '=',
'type' => 'DATETIME',
)
)
));
ADDING FOR INFO :_
And if we convert the time stamp "1578304800" to date it get 2020-01-06 10:00:00
And i need to compare only the date with the given timestamp in table field ( _form )>
I need to find the 2020-01-06 ** Only the date part from the date and time in the table**>>
If this can be done with the custom mysql also? please suggest.
You're using 'value' => date('c', $theBookingTime),
The 'c' corresponds to the output format of your date. What you're looking for is 'value' => date('Y-m-d', $theBookingTime), where Y will output the year in 4 digits, m will output the month and d - the day.
Related
In table {$wpdb->prefix}postmeta I have optional custom product field limit_time. This field store datetime in specific format as text (ex. 8.12.2020 10:00 or 19.5.2021 11:30). I need to create a filter in woocommerce so that only products that either do not have this attribute filled in or the specified date is in the future are displayed (exclude all products with this date in past).
However, I can't create the meta_query condition I need.
This way it is possible to convert that value in the database to a date, but I don't know how to use it in meta_query.
SELECT meta_value, STR_TO_DATE(meta_value,'%d.%m.%Y %h:%i') as meta_value_fix
FROM `wordpress_postmeta` WHERE meta_key = 'limit_time'
This returning correct date 8.12.2020 10:45 2020-12-08 10:45:00 but how to compare this calculated value with current datetime in functions.php?
Is it possible to use such a conversion here, with meta_query?
add_filter( 'woocommerce_product_query_meta_query', 'custom_woocommerce_filter' );
function custom_woocommerce_filter( $meta_query, $that ){
$meta_query[] = array(
'key' => 'limit_time',
'compare' => '>',
'value' => date("d.m.Y H:i"),
'type' => 'DATE' // NOT WORKING
);
return $meta_query;
}
I have a field in the database to store the date of birth of a user in time stamp format, now i want to add a search filter in which only that users are shown whose date of birth between the two min or max limits. for eg. minimum=20 years and maximum=60 years. How i can compare the time stamp date with these two parameters?
I tried out with wordpress wp_query like follows:
$current = time();
$age_min = strtotime(date('Y')-$_REQUEST['age_min']);
$age_max = strtotime(date('Y')-$_REQUEST['age_max']);
//echo $age_min.'==='.$age_max;
$age = array($age_min,$age_max);
$bars = explode(',',$_REQUEST['bars']);
$radius = $_REQUEST['radius'];
$args = array('orderby' => 'registered',
'meta_key' => 'active',
'meta_value' => '1',
'order' => 'ASC',
'count_total' => true,
'meta_query' => array(array('key' => 'date_of_birth',
'value' => $age,
'compare' => 'BETWEEN',
'type' => 'DECIMAL',),));
$user_query = new WP_User_Query(array($args,'exclude'=>array($user_id) ));
Convert the strtotime back to dates in Min and Max values :
I have converted back to Y-m-d you can convert it to your used DOB
$age_min = strtotime(date('Y')-$_REQUEST['age_min']);
$age_max = strtotime(date('Y')-$_REQUEST['age_max']);
//echo $age_min.'==='.$age_max;
$age_min = date('Y-m-d', $age_min);
$age_max = date('Y-m-d', $age_max);
And then pass it to arrays :
$age = array($age_min,$age_max);
I have a multidimensional array like this:
$array = array(
0 => array(
'name' => 'first element',
'start' => '30/04/2015',
'end' => '30/06/2015'
),
1 => array(
'name' => 'second element',
'start' => '01/07/2015',
'end' => '30/09/2015'
),
2 => array(
'name' => 'fourth element',
'start' => '01/10/2015',
'end' => '15/12/2015'
)
);
I need to select one array subarray (item) based on the today date.
today date must be between start date and end date keys.
In the end I would like to have this:
$selected_subarray = array (
'name' => 'first element',
'start' => '30/04/2015',
'end' => '30/06/2015'
)
I use to check between two dates like this:
function check_if_in_range($start_date, $end_date, $today_date)
{
$start_d = strtotime($start_date);
$end_d = strtotime($end_date);
$today_d = strtotime($today_date);
return (($today_d >= $start_d) && ($today_d <= $end_d));
}
I tryed to follow suggestions from this question How to search by key=>value in a multidimensional array in PHP
but if I'm able to filter for a key = value, I'm not able to do the same using the "check_if_in_range" function.
You do know that 30/06/2015 is invalid date, and strtotime() will return false? See here. Format mm/dd/yyyy is an American month, day and year. So your format is non-standard.
Best way is to convert it to standard format, and then use strtotime()example or just use DateTime::createFromFormat()example.
After you learn how formating and converting dates works, then you can just do simple foreach loop, and break on first found result. Here is a little demo.
Try something like the following
foreach($array as $key => $value) {
if(check_in_range($value['start'], $value['end'], $today_date)) {
$selected_subarray = $value;
}
}
I am trying to make an area on my Wordpress site that checks the date field on each of my custom post type posts each time I get on for any dates that are 7 days or less until the current date and then displays the corresponding post title. If you have questions about any of that, please just ask, it is hard to really explain it.
I would like to put an if statement in there that just says if any of the dates are 7 days or closer to the current date, display the title of that post.
Problems:
The whole the_field( 'contract_sign_date' ); function displays the date as mm/dd/yy, so I'm not sure if subtracting the_field( 'contract_sign_date' ) by the current date will even come out right.
If problem 1 would for some reason work, what if the contract sign date is the first of the month? 1 - 7 will = -6 instead of the current date.
Like I said, if you have questions about any of that, please just ask, it is hard to really explain it.
Here you go this may help you get what you may need from wordpress
$contract_date = date(the_field( 'contract_sign_date' ));
$contract_date = strtotime ('-7 days', strtotime($contract_date) ) ;
$args = array(
'date_query' => array(
array(
'after' => array(
'year' => date ('Y', $contract_date),
'month' => date('m', $contract_date),
'day' => date('d', $contract_date)
),
),
),
);
$query = new WP_Query( $args );
I have the foll code as:
$table_project_win = new Application_Model_DbTable_AfterWinProject();
$data_win = array(
'project_id' => $project_id,
'project_name' => $project,
'project_type_id' => $pro_type,
'start_date' => $dateStart,
'end_date' => $dateEnd,
'project_size' => $size,
'project_description' => $pro_des
);
$table_project_win->insert($data_win);
Here I get the $dateStart and $dateEnd variabled using as:
$dateStartt = $this->_getParam('dateStart');
echo 'date Start: '.$dateStartt;
$dateStart='"'.$dateStartt.'"';
$dateEndd = $this->_getParam('dateEnd');
$dateEnd='"'.$dateEndd.'"'
By using getParam I get the value of the date that the user has given input But when i will insert it into the database I use as
$dateStart='"'.$dateStartt.'"';
$dateEnd='"'.$dateEndd.'"'
But in the database table the value for date inserted is '0000-00-00' While when I echo the $dateStart which I have got through getParam It gives the correct value as '2012-12-11'.What is the reason of it??What Should I do??
replace $dateStart='"'.$dateStartt.'"';
with
$dateStart= $dateStartt ;
or
$dateStart='`'.$dateStartt.'`';