MySQL query for dates with CakePHP - php

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

Related

updateOrCreate does not match Carbon date

I am using Laravel Framework 6.16.0 and I am creating from a string a date object so that I can input it in my db:
$transaction_date = Carbon::createFromFormat('Y-m-d', $tradeDate);
$filling_Date = Carbon::createFromFormat('Y-m-d H:i:s', $fillingDate, 'UTC');
$product = Product::updateOrCreate(
[
'price' => trim($price),
'qty' => $qty,
'companies_id' => $company->id,
'persons_id' => $person->id,
'amount_range' => $owned,
'filling_date' => $filling_Date,
'transaction_date' => $transaction_date,
],
[]
);
When running the above query my product does not get found as $filling_Date and $transaction_date are not matched in the database, even if my product already exists.
I am guessing, the reason is that I am creating a "new" Carbon object.
Any suggestions how to match the filling_date and transaction_date in the database?
I appreciate your replies!
Try this when converting a string to a date with Carbon
$date = Carbon::parse($yourStringDate);

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?

PHP Triggering Actions Based on Dates

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

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

PHP date() function inserting zeros into database

I have an array like so which has the column names of the table on the left and the associating values on the right.
$proceduredata = array
(
'patient_id' => $patientfk,
'name_id' => $procedurenamefk,
'department_id' => $departmentfk,
'dosage_id' => $dosagefk,
'edocument' => NULL, //not implemented yet
'user_id' => $this->session->userdata('userID'),
'duration' => NULL, //not implemented yet
'submitted' => date('d-m-Y H:i:s', now()),
'comment' => NULL, //to be implemented
);
This array is then passed into a SQL insert function. The insert works fine but my "Submitted" column is getting values of this only:
0000-00-00 00:00:00
I made sure the time formats are matching? Is there something I have missed thanks.
change the date format
submitted' => date('Y-m-d H:i:s', now())
I believe that the "now()" function is a sql function and you are using PHP with the "date()" funciton. Try changing "now()" to "time()" which will give you a proper unix timestamp that the "date()" function can use to create a properly formatted date.
EDIT: I just realized I am not familiar with codeignighter, so please excuse me if the "now()" function is part of that framework.
now() is not a function, change now() to time(). Alternatively, date()'s second paramater is optional if you omit it date automatically uses the current date/time:
echo date('d-m-Y H:i:s');
will echo the current date/time in the format requested.

Categories