How to convert date format before comparison in YII? - php

I am using the default search provided by the YII CGridView.
I have a text date field in the search criteria. I am trying to compare the date passed with the date in the database but they are not matching. I need to convert the format of the dates stored in the database as the date in the database also have time and I need to remove time before comparing the dates, But I cannot figure out a way to do this.
In the default Search function() this is the line where I want to convert the dates before comparing. I have tried the conversion but this does not seem to work.
$criteria->compare(date("Y-m-d", strtotime('application_date')),
date("Y-m-d", strtotime($this->application_date)),true);
Thanks for your help!!!

There is one way of doing this, You have to use Date_Format function of mysql like follow:
$criteria->compare('DATE_FORMAT(application_date,"%Y-%m-%d")',date("Y-m-d", strtotime($this->application_date)),true);

Try formatting the date to date in the the database as ex to your desired format shown below
$criteria->compare('date1',date("d-m-Y",strtotime($this->date1),true);

Related

How to compare simple date with datetime in Laravel?

On my project, I created an option for the user to search by date, but in my database, all the inserts are in DateTime format (yyyy-mm-dd h:m:s), while the search is made only with a simple date(yyyy-mm-dd) input.
Is there any way I can compare these two values, so the user can find all the inserts on the given date?
you can use eloquent whereDate with format Y-m-d
YourModel::whereDate('created_at','2021-11-24')->get();
or without model
DB::table('your_table')->whereDate('created_at','2021-11-24')->get();

Date format for PHP sorting with Mysql (Datepicker)

I'm using DatePicker to allow users to add events into a calendar db table but I want to be able to let other users search the database and pick dates between 2 values, so I was wondering what the best date format would be for this as at the moment it's not ordering them correctly (at the moment I'm using d-m-Y). Would you have to put it in as a timestamp and then decode it with php to format it in the website or is there an easier way?
MySQL's default format for dates is YYYY-MM-DD (ISO 8601). If you work with your dates in that format you can use MySQL's default sorting to order them properly for you.

PHP datetime from string

After spending over 6 hours trying to do this and trying many different published solutions I am just going to ask the exact question.
I want to have the user enter the date and time in US format in an html form. Format for today is 12/16/2012 02:53 using 24 hour time format.
Lets call it start_date.
Then I want to insert the record including the start_date into an mysql database into a datetime type field.
I am using PHP 5.2. Many of the solutions I saw required 5.3 and none of the workarounds for 5.2 worked.
Can someone please give me an exact example.
Thank you.
Use regex or string processing to extract fields from your current format.
Create date in MySQL format.
Insert in the database.
See here : date_create_from_format equivalent for PHP 5.2 (or lower)
Actually the format of your date in not valid to be inserted in mysql table the format must be YYYY-mm-dd Hour:min:sec, in order to be place in datetime field. But if you use the field type as varchar you don't need to care about format. you can insert in whatever format you wish.
Or you can rely on MySQL parsing:
SELECT STR_TO_DATE('12/16/2012 02:53', '%m/%d/%Y %H:%i')
Note: this expects two-digit month, day and hour, i.e. 01 - not 1.
See MySQL Date format for other formats.
Also for this approach to be of practical use you will have to process failed parsing attempts: for example, you can make your Datetime column NOT NULL so that all inserts or updates fail if you tried to write NULL into it (STR_TO_DATE will return NULL for invalid date)
You asked for an example, which no one has supplied yet, so here it is:-
$start_date = date("Y-m-d H:i:s", strtotime("12/16/2012 02:53"));
echo $start_date;
Output:-
2012-12-16 02:53:00
This format matches the MySql DateTime type.
See working example here which also demonstrates that it works in PHP 5.2.
See the manual for strtotime and date.
You can use strtotime(). It parses dates according to the format. For instance dates using / (MM/DD/YYYY) are parsed using the American format, and dates using - or . (DD-MM-YYYY or DD.MM.YYYY) are parsed using the European format. See the third note in the documentation.
You really should look at upgrading to 5.4 if at all possible. There you can use the really nice date classes.

MySQL Date_Format based on today's date and another column?

I am aware of the MySQL Date_Format function but am looking to achieve the following:
I have on column with a day date in 2 digit format (01-30). I am trying to update another date formatted field with the current year, the next month (m+1) and the day field mentioned previously.
In PHP i would do this using mktime function but this must be done using mysql calls only.
Is it possible to transform in this way?
update table set field1 = concat(date_format(curdate(),"%Y-%m"),'-',field2) + interval 1 month
There is a function called STR_TO_DATE in mysql which you should be able to use to create a brand new date with using the seperate parts you described in your problem. The final input into the function should be STR_TO_DATE('01,5,2013','%d,%m,%Y') where argument one is the actual date string and argument two represents the format of the new date. You should be able to create the first argument by concatenating your parts together and then specifying whatever date format you need.

how do i get day only from javascript calender and store it into database

I had used javascript calender in my form. User have to input a date using this calender. And date is stored in the database but what I need is that only day of a date must be saved.
How can I do that as I cannot make changes in javascript code as i m not good at it.
$date_customer=date("d",strtotime($_POST['datum1']));
I had also tried it by changing the column name to "tinyint" but didn't work :( .... it only stores 127 and shows 1 when record is viewed from database.
Instead of sending date to server you could send the day by using
.getDay() method of javascript Date object.
I dont know the format of your date you get in your text input (when you click on one of the days in your calendar) but i'd suspect it to be dd/mm/yyyy or mm/dd/yyyy
So your php will need to be the following to only get the day
$date = explode("/",$_POST['datum1']);
// if format is dd/mm/yyyy then use the below
$date_customer = $date[0];
// otherwise if format is mm/dd/yyyy then use the below
$date_customer = $date[1];
Check out the explode function
i would save the date in MYSQL as an INT by using this function (save it as a unix timestamp) which would be helpful in comparing dates later on (up to the second) or add/remove days/years/months .
the idea would be send the whole date string generated by javascript to the PHP script "dd/mm/yyyy" ,
then in php using the explode function and create the unix timestamp using the mktime function
then save it to the database as an int ,
then when you want to read it , use the php date function to know the day/month/year/hour/second/minute , you could then also add hours (+3600) or days (+3600*days) etc... , or even get range of dates and many other functionalities you may use later ...
cheers
I suppose that you use mysql (TINYINT are mysql specific).
TINYINT are integer and in php integer are usually not prefixed by zeros
Use a DATE field and format it when you report it.
You can use mysql date_format(date,"%d") function in your query.
Or the php date function.
select date_format(date_field,"%d") from some_table;
You can use a VARCHAR(2) to store the date (ugly).
If you stick to store only the DAY in an int or tynint.
use sprintf("%02d",$day) to format it correctly in php.

Categories