How to compare only date and year in php codeigniter - php

My model:
public function get_payscale() {
$this->db->from('payscale P')->join('employee E','E.employee_id = P.employee_id ');
$this->db->where('P.payscale_date',date('Y-m'));
return $this->db->get()->result_array();
}
I want to compare only the month and year with the db, in which the format is y-m-d, where I want only to retrieve the y-m using the where clause.

Use like this :
$this->db->where('EXTRACT(YEAR_MONTH FROM P.payscale_date)',date('Ym'));
This query will extract the Year and month from the date given column like 201605 (2016-05-21)

Not sure with exact syntax but something like below will work for you.
$this->db->where('year(Start_Date),year(curdate())');
$this->db->where('month(Start_Date),month(curdate())');

I have used the same condition in core PHP you can simply implement it on Codeigniter if any issue gets while implementing let me know
SELECT * FROM `test` WHERE Year('payscale_date') = 2016 and Month('payscale_date') = 05

If you have datetime type in your mysql table, and if you want to grab only the year from it, use this:
$this->db->->where('year(prdt.date_updated)',$year);

Its worked for me.
$this->db->where('month(uspl_date)=month(curdate())');

Related

How to order by custom time format in laravel?

I have to order a database table with a custom time format. The time format used in the table is like this:
1:30:PM
8:35:PM
5:50:PM
7:00 PM
I tried the following, but it does not sort them all in the correct order.
->orderBy('time_schedule.start_time', 'asc');
After sorting the with the above method the result comes like
1:30:PM
5:50:PM
8:35:PM
7:00 PM
I'm not able to change the format saved, as i'm not working on that part of the application.
Also the data have to come sorted form laravel, cannot use front end sorting in my situation.
Please let me know whether there is a way to sort this with laravel.
Try this!
addSelect(\Illuminate\Support\Facades\DB::raw('TIME_FORMAT(start_time,"%H:%i") as st_time'))->orderBy('st_time','asc');
You could use STR_TO_DATE function like:
SELECT
start_time
FROM
tablename
ORDER BY
STR_TO_DATE(start_time, '%l:%i %p');
For this you have to use raw query.
Reference
You can use closer for that
->orderBy(function($item) {
return Carbon::createFromFormat('g:i a', $item->date)->format('Y-m-d H:i:s);
});
You can try SQL Date Function here:
ORDER BY DATE(time_schedule.start_time) DESC, time_schedule.start_time ASC
Hope this helps.
you can't add timestamp to database in custom format like 01:00 AM .all you to do is u need to format it to 24 hr then store it to database
so during retrieve order by clause will give you sorted order
$time = '01:00 PM';
$date = date_format(date_create($time),'H:i:s');
\App\TableModel::insert(['start_time'=>$date]);
or
DB::table('tablename')->insert(['start_time'=>$date]);
retrive by order.......
DB::table('tablename')->orderBy('start_time')->get();
for more information check ref:Insert h:mm pm/am time format into database using MYSQL

How to find the difference in days between two dates in laravel?

I have stored the date as a string in my DB in this format (dd-mm-yyyy).
Here I want to check the difference in days between the current date and the date in DB.
Here is my controller code:
public function index()
{
$domain_count = domain_details::get()->count();
//var_dump($domain_data);
$domain_alert = domain_details::
where('domain_ex_date','>',date('j-m-y'))
->get();
return view('home1')->with('domain_count' , $domain_count)
->with('domain_alert' , $domain_alert);
How do I achieve this? Is my approach right?
The above code shows 2016 is greater than 2017. I can see my logic is wrong but how do I change this?
It's better to have your dates in a DATE column in a proper format, otherwise MySQL won't know how to calculate it. Since you don't, you'll have to convert it with str_to_date, passing in the raw command:
where(DB::raw("str_to_date('domain_ex_date','%d-%m-%Y')"),'>',date('Y-m-d'))

How to get the server date (i.e month and day) in magento?

Is there any magento way to get the server date i.e day and month? I have tried this:
$today = date("m-d", Mage::getModel('core/date')->timestamp(time()));
this will return (For eg: 5-22), while i convert this date to (may 22) by
$monthdates=date("M-d",$today);
echo $monthdates;
it is returning (Jan 22).I don't know how. Can anyone clarify it, or either can show any other method to achieve what is required?
Any help would be appreciated.Thanks in Advance.
You can also try:
$today = Mage::getModel('core/date')->date('Y-m-d');
it seems you are making mistake in passing argument in date() 2nd parameter should be time string and you are passing date.
It should be like this:
$monthdates = date("M-d",Mage::getModel('core/date')->timestamp(time()));

PHP format date from returned data

I have a query that is returning a grid. One of the columns brings back a column with a date, like this:
echo "<td>{$Row[ETA]}</td>";
This displays the ETA from the database like this:
2013-10-30 20:00:0
I basically want to remove the TIME portion and just keep the date. Can this be done in the TD or do I have to the conversion elsewhere? I would like to just do the conversion within the cell, if possible.
Let me know how this can be done.
You can use the strtotime() and date() functions to achieve this!!!
date("Y-m-d", strtotime($Row[ETA]));
Well! if you want to get just date then you should use this function in your query
DATE(date_field)
as this will return only date from the datetime column
Ideally you should listen to the suggestion by JohnConde because it will limit your overhead between the database and your script but you can also substr() on the fly if you wish like this:
echo "<td>".substr($Row['ETA'], 0, 10)."</td>";
You have to echo it differently:
$eta = $Row['ETA'];
$etaDate = date("Y-m-d", strtotime($eta));
//now use $etaDate
You can use the date function to format the time, as what you're getting is a date as a string, you can use strtotime.
I think the format you're looking for is: date("Y-m-d", strtotime($Row["ETA"]));, you can either parse that into a variable and save it there, or you can concatenate the results together for the final string.

How to use the mysql "LIKE" syntax with a timestamp?

I want to allow my users to search the database for a row that was submitted on a certain day. The date is entered into the field in the database using the date() function which is great, but when i use the php strtotime function, of course the dates are not exactly the same.
Is there some clever mysql function that can help me with this?
I had an issue with this before.
You're best to generate a start and end date then use the BETWEEN function instead.
So something like this:
$start_date = "2009-01-01 00:00:00";
$end_date = "2009-01-01 23:59:59";
$sql = "SELECT * FROM table WHERE date BETWEEN '$start_date' AND '$end_date' AND id = 'x';
Of course you would just pull your dates from the DB using strtotime and append the time stamps - depends how you used date()
Hope this helps :)
You can use MySQL's DATE() function to extract date part of the timestamp:
select ... from table ... where DATE(date_column) = '2010-01-25';
If you have problem submitting '2010-01-25' from PHP, you can use PHP's date function with 'Y-m-d' as parameter to only get the date part.
$d = date('Y-m-d', strtotime(...));
Looking at your question closely, it seems you'll need both of those. First use PHP's date function to get only the date part and then use MySQL's date to match only those records.
PHP:
$timestamp_from_php = strtotime('December 25, 2009');
SQL:
select
`fields`
from
Table t
where
date_format('Y-m-d', t.`datetime_field`) = date_format('Y-m-d', '$timestamp_from_php')

Categories