I have displayed a date in a php page which is in dd-mm-yyyy format like-
$bdate = new DateTime($_SESSION['booking_date']);
$date2=$bdate->format('d-m-Y');
echo $date2;
whic gives output as "23-04-2013".
but i want this like "23rd April 2013". How can i do this in php?
Check out date() formatting strings to understand how DateTime::format() works.
print $bdate->format('jS F Y');
Try this code
$bdate = new DateTime($_SESSION['booking_date']);
$date2=$bdate->format('l jS F Y');
echo $date2;
Do you consider in use Javascript ?
You can use the moment.js
[year, month, day, hour, minute, second]
moment([2010, 1, 14, 15, 25, 50]); // February 14th, 3:25:50 PM
For example... but you can find all in www.momentjs.com
this will work for you
$date2=$bdate->format('jS F Y');
echo $date2;
where
j - Day of the month without leading zeros
S - English ordinal suffix for the day of the month, 2 characters
F - A full textual representation of a month, such as January or March
Y - A full numeric representation of a year, 4 digits
Related
I have date in this type of format: April 1st 2017 and I want to convert it into this type of format: 2017/04/01 in my CodeIgniter code using php. I have used below posted piece of code but it is not working. Please solve the issue.
Code:
$date = DateTime::createFromFormat('m/d/Y', "April 1st 2017");
echo "Date = ".$date->format('Y-m-d');
You can use strtotime() and date() php functions as
$newDate = date("m/d/Y", strtotime("April 1st 2017"));
Or in CodeIgniter
$date = DateTime::createFromFormat('j F Y - H:i', 'April 1st 2017');
echo $date->format('m/d/Y H:i:s');
Your format can be used in the constructor of DateTime. See accepted formats.
$date = new DateTime("April 1st 2017");
echo "Date = ".$date->format('Y-m-d');
Outputs:
Date = 2017-04-01
If you want to use DateTime::createFromFormat(), you have to use the proper format
"F jS Y"
The format you specified for your date is incorrect.
It would convert '04/01/2017' but it does not suit
April 1st 2017.
Try instead: createFromFormat('F dS Y')
Explanation:
F - full textual representation of a month, such as January.
d - day
S - English ordinal suffix for the day of the month
Y - 4-digit representation of year
you can try this also
<?php
$date='22 march 2018';
echo date('m/d/Y', strtotime($date));
?>
I have a feed which gives feed in the following format: "Fri 14 Oct"
I want to see if today's date matches the date from the feed. My problem is the format of today's date/
$today = date("d m");
This outputs 17 10.
What is the best way to format $today so that it outputs Day (shorthand) space date (number) Month (shorthand) ?
how about:
$today = date("D j M");
As explained in date() reference manual.
Anyway you should be aware of timezone issues unless you are 100% sure that your server is in the same timezone of the feed you are comparing.
I would follow a different approach though, you can parse the feed's date using DateTime::createFromFormat() which also understand timezones, and then compare it with today's date.
$today = date("D d M");
PHP Date Documentation
<?php
// Prints the day
echo date("l") . "<br>";
// Prints the day, date, month, year, time, AM or PM
echo date("l jS \of F Y h:i:s A");
?>
For more details, please visit http://www.w3schools.com/php/func_date_date.asp
Would be grateful for any assistance with this.
I have:
$value = gmdate("F d Y", getlastmod());
This is returning (for example):-
June 24 2016
My question is how to extract the dayofweek information out of $value?
My output needs to look like:
Friday, June 24, 2016
Is there a way to extract the dayofweek, Month, dayofmonth and year out of $value? If not, how can I get those values from 'getlastmod' ?
Many thanks
First you will need to use strtotime to convert your current $value to timestamp.
Then by using the date()function you can get the day of the week.
Example:
<?php
echo date("l", strtotime("June 24 2016")); // Output: Friday
Try this instead:
$value = gmdate("l, F d, Y", getlastmod());
Output:
Friday, June 24, 2016
$myDate = date_create(getlastmod());
echo $myDate->format('L, F d, Y');
This will print Friday, June 24, 2016
More on date/time formats here:
http://php.net/manual/en/function.date.php
From the PHP formatting guide for date (gmdate is the same as date except it is using GMT) you can use "l" to get the full text of the day of the week.
http://php.net/manual/en/function.date.php
So in your example I think the following should work to meet your output needs.
$value=gmdate("l, F d, Y",getlastmod());
I can't convert date to format d/m/Y.
date("d-m-Y", strtotime(substr($code, 22, 6)) ); return 21-10-2014
but date("d/m/Y", strtotime(substr($code, 22, 6)) ); return 21/10/2014
How to format?
I would use DateTime to create an object with your format, and then reformat it.
<?php
$date = DateTime::createFromFormat('ymd', '140521');
echo $date->format('d/m/y'); //Output: 21/05/14
https://eval.in/208227
How would you like your formated date be displayed? The d, m and y in "d-m-Y" or "d/m/Y" are responsible for what you wish to display, not - or /
Here is a few list of parameters and their output:
d : Day of the month, 2 digits with leading zeros 01-31
m : Numeric representation of a month, with leading zeros 01-12
Y : A full numeric representation of a year, 4 digits
F : A full textual representation of a month, such as January or March
l : A full textual representation of the day of the week Sunday through Saturday
If you want to display 21 October 2014, you will have to use:
date("d F Y", strtotime(substr($code, 22, 6)) );
$originalDate = "2014-10-21";
$newDate = date("d/m/Y", strtotime($originalDate));
(see strtotime and date docs on the PHP site).
The following code is producing a wrong conversion of a Timestamp (1350553368):
$dateTime = new DateTime();
$dateTime->setTimeStamp(1350553368);
echo $dateTime->format('F n, Y');
PHP converts it to October 10, 2012: http://codepad.viper-7.com/clum0f
However, that timestamp is actually for October 18, 2012: http://www.onlineconversion.com/unix_time.htm
I'm sure it's me, and not PHP, so what am I doing wrong? The code is pretty straightforward, so I can't figure it out.
You are using format 'F n, Y'. n is a numeric representation of the month (October is month 10). Use d (leading zeroes) or j (no leading zeroes). See PHP date() reference.
echo $dateTime->format('F d, Y');
Form PHP DOC
n = Numeric representation of a month, without leading zeros
d = Day of the month, 2 digits with leading zeros
You should replace
$dateTime->format('F n, Y');
With
$dateTime->format('F d, Y');