How to compare simple date with datetime in Laravel? - php

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

Related

Using Laravel and Carbon

I have a field in my database called "deadline" which is of DATE format, and I want to use Eloquent to say that if the deadline field does not match Carbon::now(); but isn't in the future then don't show this row.
How can I achieve this?
Select records where deadline is greater than or equal to today's date() in the Y-m-d format.
Model::where('deadline','>=', date('Y-m-d'))->get();
Laravel works with Carbon for formatting timestamps etc, which you can easily set the deadline column as one of those Carbon objects with a Laravel date mutator allowing you formatting abilities.
But for a select statement, i'd just use the above personally.

why do i need to use strtotime() to use date() with mysql

I am using this code successfully, but is there a way to run this where I don't need to convert my variable from a string to a number?
I am confused and wondering if I am doing it efficiently because the mysql timestamp datatype is a string but the php timestamp is a number. There should be a date format conversion for the mysql timestamp,no?
date(DATE_ATOM, strtotime($program->start_time))
the date function takes a timestamp long datatype but mysql stores timestamps in a datetime string format. Is there another way to do this where I just convert once?
I don't know why do you need to change the dates in your code.
In Laravel base Model, dates works as Carbon objects. When that fields are stored in database, they're mutated to database date format. That's because date fields have their owns getter/setters in base model.
You don't need to change your custom date fields formats, only declare them in the $dates Model propertie so it can mutate that fields when they are stored in database.
For example, in this User model class the attribute start_time will be mutated from Carbon to MySQL format when it will be saved.
Class User extends Eloquent
{
protected $dates = ['start_time'];
}
You can read more about that here http://laravel.com/docs/4.2/eloquent#date-mutators
I expect that would help you to understand it better.

Timestamp and form input date/time

I have not got any code built yet as I need to ask this question before I can start making it.
first what I am doing:
I am going to be making a tournament system on my website and I would like the tournament creators to choose a date and time that tournament will be active for signups and closed for signups.
I believe I will be using www.jongsma.org datepicker as I think it is very nice looking and easy to use for the end user. Link:here
The Question:
After sanitizing the input from the forms date/time do I need to specify for it to be converted from the users (Person inserting the time) local time to UTC before I store the data on the database or does it automatically convert the input from there local time to UTC when the data is being written to the database?
I am using MySQLite
MySQL accepts datetime in this format "Y-m-d H:i:s".
You can always convert different formatted dates into unix_timestamp with strtotime and turn it into mysql date format with:
$unix_time = strtotime($differentFormattedDate);
date("Y-m-d H:i:s",$unix_time);

How to convert date format before comparison in YII?

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

Which of these scenarios is faster for retrieving a formatted date?

I have a DATETIME string and I need only the DATE in my script to perform some searches in my database. Currently, I have two scenarios in my mind, but don't know which of them is faster.
The first scenario:
In my MYSQL database, I have two columns: datetime (which is a DATETIME type) and date (which is a DATE type).
Then, in my PHP script, each time I save a record, I will insert my known string to the datetime field, and then convert it to fit the date field (I was thinking of something like: $date = date("Y-m-d", strtotime($datetime))).
This way, all the necessary pieces are stored in my database and I can retrieve them on the fly (both the datetime and the date fields).
The second scenario:
The MYSQL database should consist only of the datetime column.
My PHP script will insert the known string to the datetime field without any other modifications.
And when I retrieve my data, I would do something like: SELECT datetime, DATE(datetime) FROM ...
Conclusion
Which of these scenarios is faster and therefore should be used? Should date formats be made on save or on retrieve? Is MYSQL faster than PHP on formatting dates? Is it better to store everything in the database and retrieve as it is, or store only the minimum and format on retrieve? Which of these scenarios is the best practice?
Thank you!
It depends of your usecases:
If you are only going to need the date for reading, then go with a single datetime column, conversion from datetime to date is cheap enough.
If you are going to select rows at a given date (like WHERE date = '2011-08-01'), then go for a date column, as this will allow mysql to use the indexes on the date column if you have added one.
If you are going to select rows in a date range, then go for a datetime column. You could do things like WHERE datetime >= '2011-08-01' AND datetime < '2011-08-16'.
The second one is the best and fast as you are getting the value based on the requirement. Rather getting some value and working on it later.
imho
datetime, or even unsigned integer (unix timestamp) is better for range filtering
datetime allow date-time function, it could be useful for aggregate function
avoid formatted data from mysql (that's mean raw)
anything related to presentation is PHP duty
Definitely depends on your situation - if you will be reading (a lot) more than writing, you can store both. But I'd go for storing one field (datetime) and convert that, either in PHP or while retrieving it from MySQL (convert datetime to char in the format you like)

Categories