I am using this code:
Carbon\Carbon::parse($quotation[0]->created_at)->format('d M Y')
The output is:
10 Mar 2016
I want:
10 March 2016
I have looked at the Carbon docs and googled high and low, and I can not find it anywhere.
If I understood you correctly:
Carbon\Carbon::parse($quotation[0]->created_at)->format('d F Y')
By the way, I found it in php date docs, Carbon uses it to generate date.
https://www.php.net/manual/function.date
$date1 = '2020-03-05';
$date2 = Carbon::parse($date1)->format('d F y');
dd($date2);
This is how it should be used, if you wish to have the full month name in the date:
{{\Carbon\Carbon::parse($client[0]->event_date_from)->format('d F Y')}}
Use f instead of M in the format function.
Related
I have on my DB the dates that I can filter like this:
select *
where
a.y=2021 and a.m=2 and a.d=7
However if I run this query tomorrow I'll have to go there and change manually.
Is there a way to do this automatically as in if I run the query tomorrow I'll get d=8 and the day after d=9 and so on?
I tried to use get date but I get the following error:
SQL Error [6]: Query failed (#20210207_153809_06316_2g4as): line 2:7: Function 'getdate' not registered
I also don't know if that is the right solution. Does anybody know how to fix that?
you can use NOW to get the current date, and use YEAR , MONTH , DAY to get parts of the date
SELECT *
FROM [TABLE]
WHERE a.y=YEAR(NOW()) and a.m=MONTH(NOW()) and a.d=DAY(NOW())
The best solution is to have a date column in your data. Then you can just use:
where datecol = current_date
Or whatever your particular database uses for the current date.
Absent that, you have to split the current date into parts. In Standard SQL, this looks like:
where y = extract(year from current_date) and
m = extract(month from current_date) and
d = extract(day from current_date)
That said, date functions notoriously vary among databases, so the exact syntax depends on your database.
For instance, a common way to write this in SQL Server would be:
where y = year(getdate()) and
m = month(getdate()) and
d = day(getdate())
I finding best practice solution to fetch rows from database using this conditions:
Last row from year 2012
All rows from year 2013
and first row from 2014
Is this solvable by one query in Rails.
I have no idea how to do it by one query but only using 3 queries.
Thanks.
This is one way to do it. I'm not saying it's the best or super optimized though, depends on your data and indexes.
#measurements = Measurement.find_by_sql(
%{select * from measurements
where date_part('year', date) = 2013
or date = (select max(date) from rounds where date_part('year', date) = 2012)
or date = (select min(date) from rounds where date_part('year', date) = 2014)
order by date})
Edit: I see you're using sqlite, this works for Postgres but should be easily portable.
This has worked for me, only it was with postgres and YEAR(dt) was date_part('year',dt). Plain and simple...
Measurement.where("YEAR(dt)=2013 or dt=? or dt=?",
Measurement.where("YEAR(dt)=2012").maximum(:dt),
Measurement.where("YEAR(dt)=2014").minimum(:dt))
I already raided the net , yet still no answers.
Well, I'm creating a hotel room reservation system.
My room availability query works fine with this query :
SELECT r.roominv_id, r.room_id, m.room_type,r.room_number,r.room_status
FROM roominventory r
INNER JOIN room AS m ON m.room_id = r.room_id
WHERE m.room_type LIKE '%$roomtype%'
AND r.room_status = 'available'
AND r.roominv_id NOT IN (SELECT b.roominv_id
FROM reserve b
WHERE NOT (b.chckout < '$chckin'
OR b.chckin > '$chckout'))
I wanted February 2 as my start date in afternoon (hotel reservations must always start after 12 noon) and February 3 as my end date ( before 12 noon )
but in my current query , if I choose February 2 as startdate and February 3 as enddate , in database, it will appear as a 2 day reservation.
So my question is:
how will I query that clients can still choose the end date from a already reserved room?
I actually saw a site that have a datepicker that have somewhat like AM/PM http://reservations.directwithhotels.com/reservation/selectDates/148/campaign/ew
BUT I'm so noob at javascript.
Depending on your data type for chckout and chckin, you may just want to use the BETWEEN operator, or use inclusive equality operators (>=, <=).
I think you just need to change the where clause to:
WHERE NOT (b.chckout < '$chckin'
OR b.chckin >= '$chckout')
This assumes that chckin and chckout are stored as dates. Otherwise you need to convert them to dates, which depends on the database . . . here are some ways:
date(b.chckin)
cast(b.chckin as date)
trunc(b.chckin)
How best store year, month, and day in a MySQL database so that it would be easily retrieved by year, by year-month, by year-month-day combinations?
Let's say you have a table tbl with a column d of type DATE.
All records in 1997:
SELECT * FROM tbl WHERE YEAR(d) = 1997
SELECT * FROM tbl WHERE d BETWEEN '1997-01-01' AND '1997-12-31'
All records in March of 1997:
SELECT * FROM tbl WHERE YEAR(d) = 1997 AND MONTH(d) = 3
SELECT * FROM tbl WHERE d BETWEEN '1997-03-01' AND '1997-03-31'
All records on March 10, 1997:
SELECT * FROM tbl WHERE d = '1997-03-10'
Unless a time will ever be involved, use the DATE data type. You can use functions from there to select portions of the date.
I'd recommend the obvious: use a DATE.
It stores year-month-day with no time (hour-minutes-seconds-etc) component.
Store as date and use built in functions:day(), month() or year() to return the combination you wish.
What's wrong with DATE? As long as you need Y, Y-M, or Y-M-D searches, they should be indexable. The problem with DATE would be if you want all December records across several years, for instance.
This may be related to the problem that archivists have with common date datatypes. Often, you want to be able to encode just the year, or just the year and the month, depending on what information is available, but you want to be able to encode this information in just one datatype. This is a problem which doesn't apply in very many other situations. (In answer to this question in the past, I've had techie types dismiss it as a problem with the data: your data is faulty!)
e.g., in a composer catalogue you are recording the fact that the composer dated a manuscript "January 1951". What can you put in a MySQL DATE field to represent this? "1951-01"? "1951-01-00"? Neither is really valid. Normally you end up encoding years, months and days in separate fields and then having to implement the semantics at application level. This is far from ideal.
If you're doing analytics against a fixed range of dates consider using a date dimension (fancy name for table) and use a foreign key into the date dimension. Check out this link:
http://www.ipcdesigns.com/dim_date/
If you use this date dimension consider how easily it will be to construct queries against any kind of dates you can think of.
SELECT * FROM my_table
JOIN DATE_DIM date on date.PK = my_table.date_FK
WHERE date.day = 30 AND
date.month = 1 AND
date.year = 2010
Or
SELECT * FROM my_table
JOIN DATE_DIM date on date.PK = my_table.date_FK
WHERE date.day_of_week = 1 AND
date.month = 1 AND
date.year = 2010
Or
SELECT *, date.day_of_week_name FROM my_table
JOIN DATE_DIM date on date.PK = my_table.date_FK
WHERE date.is_US_civil_holiday = 1
How would I go about doing a query that returns results of all rows that contain dates for current year and month at the time of query.
Timestamps for each row are formated as such: yyyy-mm-dd
I know it probably has something to do with the date function and that I must somehow set a special parameter to make it spit out like such: yyyy-mm-%%.
Setting days to be wild card character would do the trick but I can't seem to figure it out how to do it.
Here is a link to for quick reference to date-time functions in mysql:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
Thanks
I think EXTRACT is the function you are looking for:
SELECT * FROM table
WHERE EXTRACT(YEAR_MONTH FROM timestamp_field) = EXTRACT(YEAR_MONTH FROM NOW())
you could extract the year and month using a function, but that will not be able to use an index.
if you want scalable performance, you need to do this:
SELECT *
FROM myTable
WHERE some_date_column BETWEEN '2009-01-01' AND '2009-01-31'
select * from someTable where year(myDt) = 2009 and month(myDt) = 9 and day(myDt) = 12