Adjust time format in Laravel? - html

This is my code:
{{ date ('M-d-Y',strtotime($page->created_at)) }}
The date shows up like this: May-23-2018
How can I modify the code so the date shows up like this: Posted on May 23, 2018

All character meaning : http://php.net/manual/en/function.date.php
You can change symbols in your format how ever you like as long as you have correct characters(from table).
<span>Posted on {{ date('M d, Y',strtotime($page->created_at)) }}</span>
Also Laravel comes wihh Carbon extension for DateTime and is easy to use for working with dates: https://carbon.nesbot.com/docs/

You can solve this in 2 ways:
Simple way: Have 3 variables for a date, month and year and create the string the way you want.
Use date_format: Check https://www.w3schools.com/php/func_date_date_format.asp

you can try this.I hope it will help you.
{{ date("F jS, Y", strtotime($page->created_at)) }}

Related

Format date sql

Good day , i'm kind of struggling at the moment and this might be really easy for you , but i'm really new in sql .
I'm trying to change the format of a date from 2021-10-05 to Tue-05-Oct . in a htm file using SQL
I'm struggling cause i do not really understand how to use the convert or formatdate with an object (Select date.birthday as object) .
I'm a bit lost :(
You can format the date using the MySQL DATE_FORMAT() function.
For example:
SELECT DATE_FORMAT(`myDate`, '%a-%d-%b') as textDate from `myTable`
which will return a date in the form Tue-05-Oct.
You could also do this directly in crystal reports by creating a formula field as follows.
totext({ReplaceWithDataFieldToFormat},"ddd-dd-MMM")

MySQL date format to string

I have a dateformat but to display on front end I would like to display the data like so.
SELECT STR_TO_DATE('5,2013','%m,%Y');
The result I would like to generate is 'May 2013'.
Why are you storing dates as string values? Mysql has dedicated data types for date and time values: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-types.html
When using date, you can easily use DATE_FORMAT and set %m,%Y as formatting (second argument).
SELECT replace(date_format(str_to_date('5,2013','%m,%Y'),'%M-%Y'),'-',' ');
As to the format just read the docs: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format. You need an uppercase M for the month name. And use DATE_FORMAT to get from a date to a string.
SELECT DATE_FORMAT(DATE '2013-05-01', '%M %Y');
Assuming that you have 5,2013 stored in your database, you need to use DATE_FORMAT after parsing the string:
SELECT DATE_FORMAT(STR_TO_DATE('5,2013','%m,%Y'), '%b %Y')
Why? Because it seems you don't have a date type stored in the database. So you parse it to date using STR_TO_DATE. This give you the default date format. DATE_FORMAT let you apply other formattings on it.
All avaliable formatting values are documented here: https://www.w3schools.com/sql/func_mysql_str_to_date.asp
In general, I would recommend to think about storing date objects instead of custom date strings like 5,2013. This avoids those castings, because you could directly use DATE_FORMAT. It also has benefits on filtering/ordering and performance. Using date types, you could easily sort by it or filter e.g. everything in month X.
I don't say its not possible with custom strings like 5,2013 but would be more complex and could result in bad performance if the db grows.
You can use the functions:
str_to_date() to convert the string (after concatenating the prefix '1' to your string) to a valid date and then
date_format() to reformat the date:
SELECT date_format(str_to_date(concat('1,', ?),'%d,%m,%Y'), '%b %Y');
Replace ? with your string.
See the demo.
Result:
May 2013

sql : add euro sign and round the number

I have an application where I can download data to excel file.
The problem is that I need to add euro sigh to some fields.
I had a simialar problem with date.
In excel it was 54656434
It changed in normal date after using convert(varchar,senddate,105)
Is there something that looks like that to add euro sign and round it to 2 decimals ???
EDIT:
In excel i get the number like 1,55435
I need the output be like € 1,55
This is kind of convoluted... let me know what you think. Replace '1,55435' with your field.
SELECT CONCAT('£', CAST(CAST(1,55435 AS DECIMAL(18,2)) AS CHAR(55)))
I'm in the US, so I'm not sure if decimals can be changed to use commas instead of periods.
Edit: Also, try this: SELECT CONCAT('£', FORMAT(1,55365, 2)). Let me know which works.

CakePHP 3.2, date output

I´ve several dates wich are stored in my database like this: 2016-02-17.
I want them to be displayed like this: 17.02.2016.
For this I use this code in my AppController:
I18n::locale('de-DE');
Time::$defaultLocale = 'de-DE';
Time::setToStringFormat('dd.MM.YYYY');
Type::build('datetime')->useLocaleParser()->setLocaleFormat('dd.MM.YYYY');
But my output always looks like this: 17.02.16.
How do I force the output of the year with 4 characters?
Many thanks in advance. :)
Try using date("d.m.Y", strtotime($yourDateAsStringFromDb)). This will return the date formatted as you describe you want it.
I just tryed using this and it works to display the date as you asked.
bare in mind in this capital letters matter

How to get time (hh:mm:ss) from sql query?

I have Database with date field.
I see the time like: 1900-01-01 13:38:00.000
How i can format it like: 13:38 ?
SELECT convert(varchar, getdate(), 108) outputs as hh:mm:ss.
If you are using SQL server 2008 then,
Suppose your column name is XTime which is holding time in HH-MM-SS.ms formate. If we want to retrive time in HH-MM-SS format then write following query,
Select ID, XDate, Convert(Time(0),XTime,0) -- For HH-MM-SS format
Try this:-
RIGHT(CONVERT(VARCHAR,'DATE',100),7)
For non-military time.
I know I am late. This has already been answered by #Yes - That's Jake. It works perfect. But to help you more, I have a cheat sheet link so that you can write it in any format.
You can visit: Date and Time conversions using SQL cheat sheet
You will find a list of codes and the format that #Yes - That's Jake mentioned in his answer.