PHP Triggering Actions Based on Dates - php

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 );

Related

Wordpress query arguments on post_metavalue with date comparison ignore years greater than current year

I'm trying to have wordpress query event dates that exist now and beyond. They are stored in wp_postmeta.meta_value The query arguments that I am using only seem to handle the current year. I'm wondering if this is because the meta_value is stored as longtext.
Here are the query arguments I am using:
$query_args['orderby'] = 'meta_value_num';
$query_args['type'] = 'DATE';
$query_args['meta_key'] = '_um_groups_event_start';
$query_args['meta_query'] = array(
array(
'key' => '_um_groups_event_start',
'value' => date("m/d/Y"),
'compare' => '>=',
)
);
One of my entries is: 01/29/2021 5:30 PM
This does not appear in the results. However, if I change the month and year of the entry. Example: 05/29/2020 5:30 PM then it does appear in the results as it is greater than date("m/d/Y")
Any idea why it might be skipping over dates that appear past the current year?

Meta query compare date

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.

english textual date expression cakephp find queries

let's say that I have a database table called users in a cakephp application which contains a bunch of users and in that table I have a field called date which contains the registration date of each user. Let's say that I wanna make a find query to get all the users registered last month for example. Something like:
$last_month_users = $this->User->find('all', array(
'conditions' => array(
'User.date' => 'last month'
)
));
Something like this doesn't work. Any idea how I would do this please?
Thank you
If you're using mysql, you're looking to add in your statement:
WHERE MONTH(User.date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
I believe you can do this in cakePHP by doing the following:
$last_month_users = $this->User->find('all', array(
'conditions' => array(
'MONTH(User.date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)'
)
));

MySQL query for dates with CakePHP

I'm trying to select the values that fall between 2 dates, so I'll have to use <= and >=, however, my query is seemingly behaving as just less than or greater than and not considering values equal to the date.
I'm using CakePHP which saves dates in "Y-m-d h:i:a" format. I wanted to find dates on given week intervals (starting on Sundays), so I used the following.
$start_date = date('Y/m/d', strtotime('last Sunday', strtotime($timestamp)));
$formatted_start_date = str_replace("/","-",$start_date);
I tried to do find $start_date formatted as "Y-m-d" but then it wouldn't find the correct date, so I switched it to the way it is and used str_replace to format it to using "-" instead of "/".
$date_query = $this->Order->query("select * from orders where id = '$id' and created => '$formatted_start_date' and created <= '$current_timestamp' ");
Considering the time values in my database are in "Y-m-d h:i:a" format, can I use "Y-m-d" for date comparison? Is it possible to do a MySQL query that involves both LIKE and <= ?
No need to do a str_replace() - just get the Y-m-d:
$start_date = date('Y-m-d', strtotime('last Sunday', strtotime($timestamp)));
Then, instead of manually creating a query, use the CakePHP conventions (yes, you can use Y-m-d for date comparison even though the datetimes stored in the database are Y-m-d H:i:s)
$this->Order->find('all', array(
'conditions' => array(
'id' => $id,
'created >=' => $start_date,
'created <=' => $end_date . ' 23:59:59',
'my_field LIKE' => '%whatever%'
));
Though - this seems kind of strange - usually you're either looking for something by 'id' OR by a date range - not both. But - maybe you have a reason :)
And as you can see above, yes, you can add a 'LIKE' also if you need.
Above answer is totally correct, but you can take easier approach using cakePHP time helper, which has function daysAsSql, it transcribes and time-readable strings into database range.
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::daysAsSql
Just add condition's array like this.
$resl = $this->DBNAME->find('all',array(conditions=>array('date1>date','date1<date')));
Replace 'date' with your date.
This is worked for me.
Try this in your controller
$searchTutorQuery = $this->Tutordetails->find('all', array(
'conditions' => array(
"User.zip" => $zipcode1,
"Tutordetails.user_id"=>$uid,
"Tutordetails.subject_id" => $subjectName,
"Tutordetails.hourly_rate >=" => $hourly_rate
),
//"User.id =" => $userid,
'fields' => array('user_id', 'Tutordetails.subject_id', 'Tutordetails.hourly_rate',
'User.zip', 'Tutordetails.zip'),
'order' => array('Tutordetails.id DESC')
));

How to display only date in symfony sfWidgetFormDate but it should pass month and day as default '1'?

I have to display date alone in sfWidgetFormDate but I want date and month to be passed in hidden.
I have given as:
$this->widgetSchema['fellowship_admission_date'] = new sfWidgetFormFilterDate(array(
'from_date' => new sfWidgetFormDate(array('format' => '%year%')),
'to_date' => new sfWidgetFormDate(array('format' => '%year%')),
'with_empty' => false
))
It displaying only year but I want to pass month and day in 'from_date' and 'to_date' default as '1' as hidden.
Why don't you display the entire date, and hide day and month with jQuery?!
It could be a good and fast solutions...

Categories