display a date in particular format from month array in php
PHP got you covered :)
setlocale(LC_TIME, 'fr_FR');
$dateString = '2020-08-08';
$timestamp = strtotime($dateString);
$formattedDate = strftime('%d %B %Y', $timestamp);
//setlocale(LC_ALL, Locale::getDefault()); // restore (if neccessary)
echo utf8_encode($formattedDate);
output
08 août 2020
Working example.
To get the 1er
day you can do what you already did. Just split up the strftime(or its result).
references
- strftime
- setlocale
another solution (will display all days ordinally though)
$locale = 'fr_FR';
$dateString = '2020-08-01';
$date = new DateTimeImmutable($dateString);
$dateFormatter = new IntlDateFormatter(
$locale,
IntlDateFormatter::FULL,
NULL
);
$numberFormatter = new NumberFormatter($locale, NumberFormatter::ORDINAL);
$ordinalDay = $numberFormatter->format($date->format('d'));
$dateFormatter->setPattern('LLLL Y');
echo $ordinalDay . ' ' . $dateFormatter->format($date->getTimestamp());
output
1er août 2020
Working example.
references
- Pattern for DateFormatter
- NumberFormatter
How to convert a date having ordinal format 1st March 1994?
You have three problems.
- First, you're trying to parse the date using the format
YYYY-MM-DD
. That's not the format of your data, which is why parsing is failing. - Second, you're expecting a
Date
object to retain information about a particular format. It doesn't. Instead, you would parse from one text format to aDate
, and then use anotherDateFormat
(with the desired output format) to format theDate
into aString
.Date.toString()
will always use the same format, regardless of how you arrived at theDate
. - Third, your format of
YYYY-MM-DD
isn't really what you want - you wantyyyy-MM-dd
.YYYY
is the "weekyear", andDD
is the "day of year".
I don't know of any SimpleDateFormat
approach which would handle the ordinal part of your input string ("1st", "2nd" etc) - you'll probably need to put a bit of work into stripping that out. Once you've got a value such as "1 March 1990" you can parse with a SimpleDateFormat
using the pattern d MMMM yyyy
. Make sure you set the time zone and the locale appropriately.
Ordinal hour-of-day Hour Ending conversion to date-time value
Perhaps the java.time
method you are looking for is truncatedTo, which can be used to take any ZonedDateTime
and truncate the minutes and seconds
ZonedDateTime zdt = ZonedDateTime.now();
ZonedDateTime rounded = zdt.truncatedTo(ChronoUnit.HOURS);
ZonedDateTime hourEnd = rounded.plusHours(1);
PHP echo something based on day of month
Actually what you want to do can be done very trivial
echo (new DateTime())->format('jS');
or using date()
echo date('jS');
From date() manual:
S - English ordinal suffix for the day of the month, 2 characters
NSDateFormatter setDateFormat non-zero-padded month number
Use M
instead of MM
in the format string. If you use two letters, you specify that the result should have two digits, if you don't, the result can have one or two digits.
Formatting date to human readable format
$timestamp = "2013-09-30 01:16:06";
echo date("F jS, Y", strtotime($timestamp)); //September 30th, 2013
Note the use of S
to get the english ordinal suffix for the day.
Since you're already using strtotime
if you need a human readable data for the current time you can just use the keyword "now" as in strtotime("now")
Related sources
- Date
- strtotime
Convert date to ordinal python?
You'll need to use strptime
on the date string, specifying the format, then you can call the toordinal
method of the date
object:
>>> from datetime import datetime as dt
>>> d = dt.strptime('2010-03-01', '%Y-%m-%d').date()
>>> d
datetime.date(2010, 3, 1)
>>> d.toordinal()
733832
The call to the date
method in this case is redundant, and is only kept for making the object consistent as a date
object instead of a datetime
object.
If you're looking to handle more date string formats, Python's strftime directives is one good reference you want to check out.
Convert date format for db
Use the date function:
$time = strtotime("Friday, 21 October, 2011");
echo date("Y-m-d", $time);
The linked page provides a good description of the available format specifiers,
and you can see the predefined datetime constants here.
Format Description
================================================================================
d Day of the month, 2 digits with leading zeros
D A textual representation of a day, three letters
j Day of the month without leading zeros
l A full textual representation of the day of the week
N ISO-8601 numeric representation of the day of the week
S English ordinal suffix for the day of the month, 2 characters
w Numeric representation of the day of the week
z The day of the year (starting from 0)
Week ---------------------------------------------------------------
W ISO-8601 week number of year, weeks starting on Monday
Month ---------------------------------------------------------------
F A full textual representation of a month, such as January or March
m Numeric representation of a month, with leading zeros
M A short textual representation of a month, three letters
n Numeric representation of a month, without leading zeros
t Number of days in the given month
Year ---------------------------------------------------------------
L Whether it's a leap year
o ISO-8601 year number.
Y A full numeric representation of a year, 4 digits
y A two digit representation of a year
Time ---------------------------------------------------------------
a Lowercase Ante meridiem and Post meridiem
A Uppercase Ante meridiem and Post meridiem
B Swatch Internet time
g 12-hour format of an hour without leading zeros
G 24-hour format of an hour without leading zeros
h 12-hour format of an hour with leading zeros
H 24-hour format of an hour with leading zeros
i Minutes with leading zeros
s Seconds, with leading zeros
u Microseconds (added in PHP 5.2.2)
Timezone ---------------------------------------------------------------
e Timezone identifier (added in PHP 5.1.0)
I Whether or not the date is in daylight saving time
O Difference to Greenwich time (GMT) in hours
P Difference to Greenwich time (GMT) with colon between h and m
T Timezone abbreviation
Z Timezone offset in seconds.
Full Date/Time ---------------------------------------------------------------
c ISO 8601 date (added in PHP 5)
r » RFC 2822 formatted date
U Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
Related Topics
Rscript Does Not Load Methods Package, R Does -- Why, and What Are the Consequences
Apply a Function Over Groups of Columns
Get_Map Not Passing the API Key (Http Status Was '403 Forbidden')
R Error "Sum Not Meaningful for Factors"
How to Parse Year + Week Number in R
In R, Use Gsub to Remove All Punctuation Except Period
Create Zip File: Error Running Command " " Had Status 127
Re-Ordering Bars in R's Barplot()
Making a Stacked Area Plot Using Ggplot2
How to Select Last N Observation from Each Group in Dplyr Dataframe
Identify All Objects of Given Class for Further Processing
Change Background and Text of Strips Associated to Multiple Panels in R/Lattice
Calculating Mean for Every N Values from a Vector
Find K Nearest Neighbors, Starting from a Distance Matrix
Find Start and End Positions/Indices of Runs/Consecutive Values