I'm a noob (I confess it) and can't manage to find a solution to my problem.
I'm setting up the date format on Drupal and it uses the PHP date format.
Right now it's "d F Y", so it appears as 07 Dicembre 2021 (in Italian), but in Italian months are written out in lowercase. Is there a way to transform Dicembre into dicembre? I couldn't find a proper way.
Thanks for your help!
Using strtolower() the output will be as you expected:
<?php echo strtolower(date("M"),2);?>
So to implement it only for Month, simply break the date format into 3 parts. Day , Month, Year.
Use drupal module:
https://www.drupal.org/project/simple_field_formatter
Go to:
/admin/structure/types/manage/[YOURTYPE]/display
For your date field, click the gearwheel on the right (format settings) and activate the checkbox for strtolower
Or create your own FieldFormatter: https://www.webwash.net/how-to-create-a-custom-field-formatter-in-drupal-8/
You should use the locale aware strftime, which formats according to the current locale.
setlocale(LC_TIME, "it_IT");
echo strftime("%d %B %Y"); // 07 dicembre 2021
Related
This question already has answers here:
Conversion from MySQL date to RFC822 date format
(2 answers)
Closed 9 months ago.
I have a date field on my database. the date format as following.
June 17, 2013
Im using the format as
date("F j, Y");
So my question is there a way that i can display this date in RFC-822 format using php? or do i need to start saving the date in RFC-822 format from now on? Thanks in advance.
Using the following syntax, you can display current time in RFC822 Format.
$date = new DateTime('2000-01-01');
echo $date->format(DateTime::RFC822);
Neither.
From now on you have to start using format supplied by database.
You have to understand the difference between storage format and display formatting. It's different matters. When storing data in mysql, you have to follow mysql rules. So, instead of June 17, 2013 you have to store 2013-06-17.
And then convert at output to whatever format required - not limited to a single one but whatever format is demanded by destination.
None of the other answers worked for me, so this is what worked... to take a date in PHP and output it in RFC822:
date("D, d M Y G:i:s T", strtotime($date));
Hope that helps others.
As was pointed out your best bet is to change the way you are storing your dates to something other then a string. date("Y-m-d", strtotime($date)) can assist you in this endeavor.
But to solve the immediate need you can utilize use strtotime, date and the DATE_RFC822 constant to get you what you are looking for.
echo date(DATE_RFC822, strtotime($value));
See First example on php date documentation
As #ashleedawg and others mentioned in some comments the simplest solution that works:
date("D, d M Y H:i:s O", strtotime($date));
Mind the "H" and the "O" ;)
Thanks!
If you want to date format something in PHP for RFC-822 , then just do this...
date('r', strtotime($date))
'r' ยป RFC 2822 formatted date Example: Thu, 21 Dec 2000 16:01:07 +0200
Source: PHP.net: DateFormat
But, as other stated, you don't want to store this in your database! However, you'll need to use r for other things, like XML-RSS date time formats...
All date-times in RSS conform to the Date and Time Specification of RFC 822... (Source: RSS 2.0 Specification.)
date_format(date(your database field), '%D, %j %M %t')
and what type of format you want just see the link
date and time format for Mysql
You can save it as TimeStamp in database and show it RFC822 format
date(DATE_RFC822, time());
This is the only solution that worked for me:
date("D, d M Y H:i:s T", strtotime($date));
Other examples above that didn't work include using the DATE_RFC822 format specifier, which puts out a 2-digit year, instead of 4 digits. Then the other suggestion to use G:i:s for time doesn't work because G specifies no leading zeroes, so you'll get 2:00:00 instead of 02:00:00.
don't use T at the end but an "O", it works for me
I know this is a common problem, but I can't seem to find the solution anywhere. In my last question [complicated date functions - comparing, subtracting I needed to compare timestamps to get an accurate date for some Cisco logs.
The best I can come up with (since the dates don't actually feature the year) is in the format
Mar 1 2013 00:03:55:
from
Mar 1 00:03:55:
But when I ran some tests, strtotime is converting this date as
Jan 1970
Using
print date("M Y", strtotime($c_log))."\n";
Am I going to have to reformat it into a date it can understand? I don't appear to have the DateTime function. What's the simplest way?
Use the date_parse_from_format() function so you can specify the format
http://php.net/manual/en/function.date-parse-from-format.php
Works for me:
echo date('M Y', strtotime('Mar 1 00:03:55')); // outputs Mar 2013
it should default to the current year when one isn't included.
I'm brazilian and there's a wordpress plugin that uses
" . date("d F Y (H:i)",$date) . "
Output: 16 January 2013 (00:54)
But it should be 16 Janeiro 2013 (00:54), in portuguese... How can I change it?
PS: I think maybe the date is set by an external file provided by the plugin creator :p I'm not sure though
WordPress has date_i18n to retrieve the date in localized format, based on timestamp.
Try:
echo date_i18n("d F Y (H:i)", $timestamp);
WordPress has an extensive page on how to format date and time.
For the french language I use this
setlocale(LC_ALL, 'fra');
echo strftime("%A %d %B %Y",time());
For in portuguese
setlocale(LC_ALL, 'ptg'); //
echo strftime("%A %d %B %Y",time());
see Language Strings Country/Region Strings.
The documentation for date already answers this:
To format dates in other languages, you should use the setlocale() and
strftime() functions instead of date().
And strftime says that the way to do what is by using setlocale:
Format the time and/or date according to locale settings. Month and
weekday names and other language-dependent strings respect the current
locale set with setlocale().
That said, the C locale-aware functions do not provide sufficient functionality for languages that have cases. In such situations (i.e. most of the time) you need to roll your own.
I'm not sure that this is a bug since after searching I can't find any duplicate experiences- however, this one has me stumped.
While in the midst of a (rather painful) script that is intended to take a bunch of freetext records and convert them to useful date records, my trusty friend strtotime() seems to have let me down.
For testing purposes, I boiled the code down to this:
<?=date('Y', strtotime("1999"));?>
Output shows: 1999
<?=date('Y', strtotime("1981"));?>
Output shows: 1981
<?=date('Y', strtotime("2001"));?>
Output shows: 2012
<?=date('Y', strtotime("2021"));?>
Output shows: 2012
Everything seems fine until the input exceeds "1999"- From that point on, every year before and after the current one returns the current year (2012)
Any input is much appreciated.
As per PHP's date/time format docs:
The "Year (and just the year)" format only works if a time string has already been found -- otherwise this format is recognised as HH MM.
(2nd last note on the page).
Try prefixing the years with Jan 1,.
For example:
<?=date('Y', strtotime("Jan 1, 2021"));?> outputs 2021 as expected.
I'm supposing this is because certain years can be incorrectly parsed as month/day pairs, such as "2012" being interpreted as "December 20th of the current year".
If you want proof for yourself, try changing the date format to r:
<?=date('r', strtotime('2001'));?> gives Thu, 23 Feb 2012 20:01:00
The problem is, that it is parsed as time, what you can see if you use date('c') instead of date('Y');.
php > var_dump(date('c', strtotime("2001")));
string(25) "2012-02-23T20:01:00+01:00"
You should pass the value unambiguous for example 2012-01-01.
Another solution is to use a function, that allows to specify the format of the given input, like strptime(), or DateTime::createFromFormat()
php > echo DateTime::createFromFormat('Y', '2001')->format('c');
2001-02-23T22:29:56+01:00
Use this:
echo date('Y', strtotime("1/1/2001"));
echo date('Y', strtotime("1/1/2021"));
Using 4 digits as date can be interpreted as time, you should use a more specific format to make sure the function works as you expect it.
So 2021 is 20:21 (24h format) of 2012
$plaintext = '2004'; //'2004-11','2004-5','2004-5-1','2004/5-1','2004/08/10'
echo date('Y', strtotime(
(strlen($plaintext)==10?$plaintext:
(strlen($plaintext)==7? str_replace('-','/',$plaintext).'/01':
(strlen($plaintext)==4?$plaintext.'/01/01':$plaintext)
)
)));
Hi I am saving data from rss feed url. From that me got date time like this.
Sun, 2 January 2011 03:04:02 GMT+5:30
How to change this date to this format 2nd January 2011, 03:04 PM using php?
any body knows the solution please help me.
You can se the strtotime function to convert the existing string and the 'r' specifier to the date function as follows (looks like you want it in RFC 2822 format, if not tweak accordingly):
date('r', strtotime("Sun, 2 January 2011 03:04:02 GMT+5:30"));
Incidentally, make sure you're setting your local timezone correctly via date_default_timezone_set, etc.
The following functions are useful for taking a string and getting a timestamp back:
strtotime()
DateTime::createFromFormat()
After you have it as a timestamp, you can reformat it using date(). I'm not 100% sure if strtotime() would accept that format, but it should accept it because the format it isn't ambiguous.
echo date("js F Y, h A", strtotime($oldDate));