How to order by custom time format in laravel? - php

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

Related

How to get time with month name,date and hour?

i have used Now() and it stores something like "2017-01-10 19:28:58" in database which is the current time of user's device.
But i want it like January 10 at 7:28pm . how to do it in simple way. please help
You can use PHP date function with its formatting options
<?php
echo date("F d \a\t g:ia");
If you want more detailed formatting please visit the PHP manual here. You can find everything you need with detailed examples.
you could use the PHP date function
date(F d \a\t\ g:ia);
Your post is ambiguous, but it sounds like you are referring to the SQL NOW() function. The format of the data stored could be a DATETIME or TIMESTAMP or a combination of DATE and TIME columns. The format you get when you retrieve this value from the database depends on that data storage format, your query, and how the value is dealt with when it gets into PHP.
If you want to reformat it using SQL, consider a function like DATE_FORMAT. Assuming your column with the date is called my_column, here's a sample query.
SELECT DATE_FORMAT(my_column, '%M %e at %l:%i%p');
EDIT: you can also use DATE_FORMAT on the result of the NOW() function:
SELECT DATE_FORMAT(NOW(), '%M %e at %l:%i%p');
You might have to tweak the second parameter to get the date format you want.
If your date is stored as a string (VARCHAR or whatever) in your database, then you would need to convert it to a timestamp or datetime first and then use the PHP date function to output the variant you want. Assuming $row is a record from your data table:
$date_string = $row["my_column"];
$stamp = strtotime($date_string); // NOTE that this will assume some timezone
if (!$stamp) {
die("Could not create a timestamp from the date");
}
echo date("F j \a\t g:ia);
You could also use PHP'S DateTime functions which are more modern, if somewhat verbose in usage.

How to compare only date and year in php codeigniter

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

In PHP convert date in to time format output

In my database field there in date and time saved using time(); function.
I want to compare that one with given date in date format like '20/02/2015 '
my query is like:
select view,optin,insertTime,isUnique from sg_page_report as PR where insertTime='20/02/2015'"
where insertTime contains different format date like '1393587636'
How can I resolve this one?
WHERE FROM_UNIXTIME(insertTime)='2015-02-20'
Or you can do it like
$ts=mktime(0,0,0,2,20,2015); // or
//$ts=strtotime('20/02/2015'); // inefficient
And your query can be modified to look like
WHERE insertTime=$ts

Datetime format check in php

How would i go about checking to see if an auction has expired in my database? I have a datetime column in MySQL that i believe is of the following format: YYY-MM-DD hh:mm:ss. If this is the case would the following check work - i.e. want to select only expired auctions from the table in the database...
<?php
//Some code
$auctioncheck = mysql_query("
SELECT * FROM auction WHERE ($date_time > finish_time)
");
?>
While "finish time" is a column in the database of the above cited format. Presuming this works how actually do i get the current date into the same format? If anybody knows i would very grateful cheers. Even more so if the above query wouldn't work and something else is required. Thanks again.
Oh and of course i would define the date_time variable to start with
Do you actually need the $date_time variable? The easiest way to do this would be SELECT * FROM auction WHERE finish_time < NOW(). That way you'll get your results and don't have to set the date from PHP.
You can use
<?php
//Some code
$date_time=strtotime($date_time);
$auctioncheck = mysql_query("
SELECT * FROM auction WHERE ( $date_time > UNIX_TIMESTAMP(finish_time))
");
?>
This solution:
// to show both date and time,
$date->get('YYYY-MM-dd HH:mm:ss');
// or, to show date only
$date->get('YYYY-MM-dd')
is from this post: PHP Zend date format
and here's another solution: how to format a Date in MM/dd/yyyy HH:mm:ss format in javascript?
and to do it in php:
How to get the current date and time in PHP?
The function date is made for it:
// Get the current date
$date_time = date("y-m-d H:i:s");
Look at the documentation page to see other format flags ;)

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