I get a $date from an API in UTC format like 2021-11-13T14:00:14Z.
So using php I convert it to : 13 November 2021
$date = strftime(date('d F Y', strtotime($date);
So now I want to translate it in French.
I am working on local with on macbook pro using MAMP.
My html code setting is :
<html lang="fr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
I even forced Local Time to :
setlocale(LC_TIME, 'fr', 'fr_FR', 'fr_FR#euro', 'fr_FR.utf8', 'fr-FR', 'fra'); date_default_timezone_set('Europe/Paris');
But the date remain in english !
Thanks in advance for your help
You're not calling strftime() correctly. The first argument must be a format string. date() returns an already-formatted date, and it doesn't use locales. This works for me:
setlocale(LC_TIME, 'fr', 'fr_FR', 'fr_FR#euro', 'fr_FR.utf8', 'fr-FR', 'fra');
date_default_timezone_set('Europe/Paris');
$date = "2021-11-13T14:00:14Z";
echo strftime('%d %B %Y', strtotime($date));
Solutions based on the IntlDateFormatter class work independently of the local installations of the server. Example as already written in the comment here. When using the class dt, which uses the IntlDateFormatter class, the same format characters can be used for the method formatL as for DateTime::format.
$dt = dt::create('2021-11-13T14:00:14Z');
//UTC -> localTime "2021-11-13 15:00:14"
$dt->setTimeZone('Europe/Paris');
echo $dt->formatL('l d F Y H:i','fr');
// samedi 13 novembre 2021 15:00
echo $dt->formatL('l d F Y H:i','de');
// Samstag 13 November 2021 15:00
The date string contains the "Z" time zone, which corresponds to UTC.
A new time zone can be set with setTimeZone() to get the local time. If the UTC time (here 14:00) is to be displayed, this line must be omitted. The format method (without L) still exists. This method delivers the result as usual in English.
Related
This is the coding and it is echoing with the right format but the fact that the date is wrong when it prints out.
Output: 31 Dec 1969 19:33
Database Timestamp 2016-05-20 21:53:17
<?php
date_default_timezone_set('ECT');
$timestamp = 1456778973;
echo date('d M Y H:i',$row['timestamp']);
?>
and i have tried doing the date in different ways and still the same result
In the code sample you posted, $row['timestamp'] has not been set, so the date is constructed with timestamp 0, also known as epoch, or the date that is being echoed.
If you change it as follows, it should work fine:
<?php
date_default_timezone_set('ECT');
$timestamp = 1456778973;
echo date('d M Y H:i', $timestamp); ?>
Side note:
Time zone ECT is not a valid time zone code in PHP. If I assume correctly that you mean european central time, you would have to specify CET instead.
ECT doesn't exist as a valid TimeZone, did you mean CET perhaps?
The correct way to do this is using the DateTime class, i.e.:
$date = new DateTime();
$date->setTimestamp(1456778973);
$tz = new DateTimeZone("America/Denver");
$date->setTimezone($tz);
echo $date->format('d M Y H:i');
PHPFiddle Demo
Note:
Dates should always be stored in DB as UTC (timestamp aka unix time), then you can add or subtract the timezone offset using the DateTime class.
Would you know what the offset would be for mountain standard time?
Mountain Time: America/Denver
Mountain Time (no DST): America/Phoenix
List of Supported Timezones
I m referring to this: Converting ISO 8601 format to d M Y in PHP
I tried this same as this: echo date("d M Y H:i:s", strtotime($time));
But time is not shown as saved in database. Its showing few hours difference.
Database has: 2016-03-20T23:30:51+00:00
With above php echo i get: 21 Mar 2016 00:30:51
Where as it must be 20 Mar 2016 23:30:51
Above example shows additional of 1 hour y?
I tried using this format to display hour & minute but time shown is wrong. Time display has few hours difference. Why is this so?
Your date format 2016-03-20T23:30:51+00:00 reveals a GMT DateTime (side note: the same TimeZone used by php/unix timestamps).
So when you write:
echo date( "d M Y H:i:s", strtotime( $time ) );
You obtain yet the correct date converted in your system TimeZone.
You can use DateTime class to perform more operations with dates:
$date = new DateTime( $time );
echo $date->format("d M Y H:i:s");
will print the date in original (GMT) format.
$date->setTimezone( new DateTimeZone('Europe/Berlin') );
echo $date->format("d M Y H:i:s");
will print the date in 'Europe/Berlin' Timezone.
Side note: saving dates in ISO 8601 UTC/GMT is actually the best choice.
Read more about DateTime
Read more about DateTimeZone
Your system is using its local timezone instead of UTC, whereas the database is in UTC. If you wish to display in UTC, you can use http://php.net/manual/en/function.date-default-timezone-set.php to set the timezone to UTC before calling strtotime and date.
I am using a plugin to create wordpress posts from a twitter feed, and I am trying to edit it so the published time is the same as the tweeted time, rather than the time the cron was run.
Unfortunately, twitter's API returns an already formatted date string, instead of a timestamp, so I am having to parse it and then save it in a wordpress friendly format.
// Wed Jun 06 20:07:10 +0000 2012 (Twitter formatted date example)
// 2014-03-10 18:30:26 (Wordpress formatted date example)
$tweet_date = $tweet->created_at;
$tweet_date = date_create_from_format("D M d h:i:s O Y", $tweet_date);
$tweet_date = date("Y-m-d h:i:s", $tweet_date);
Unfortunately, all I am getting from this the Unix Epoch (Jan 1st, 1970).
I know I must be missing a step, but I can't figure out where.
You had two issues:
1) You were using h for hours when you meant H for 24 hour periods
2) You need to use date_format() when using date_create_from_format() as that function returns a DateTime object which is not compatible with date()
$tweet_date = date_create_from_format("D M d H:i:s O Y", 'Wed Jun 06 20:07:10 +0000 2012');
echo date_format($tweet_date, 'Y-m-d H:i:s');
See it in action
The problem is because you're mixing and matching between PHP's old and new-style date handling.
date_create_from_format() is part of the newer API, and outputs a DateTime object, not the timestamp integer that the older date() function is expecting.
Ideally you should stick entirely with either the new or the old date functions. You can switch between them, but there usually isn't a need to.
For example, in your case, the DateTime object generated by date_create_from_format() has a perfectly usable format() method attached to it, which does exactly the same as the date() function, but on a DateTime object.
$tweet_date_object = date_create_from_format("D M d h:i:s O Y", $tweet_date);
$tweet_date = $tweet_date_object->format("Y-m-d h:i:s");
My date is ahead 1 hour when I post to mySQL via php. I'm in Los Angeles. How can i make the time correct?
Here is what I currently have:
$date = date("m/d/y g:i A") ;
Use this to get the time zone PHP is using date_default_timezone_get();
If you're in Los Angeles you can set your time zone temporarily for only the current script
date_default_timezone_set('America/Los_Angeles');
http://php.net/manual/en/function.date-default-timezone-set.php
Also pass T in the date function:
echo date("D M j G:i:s T Y"); outputs Mon May 25 16:23:49 EDT 2009
see
date_default_timezone_set
like
date_default_timezone_set('America/Los_Angeles');
First check if servers time is correct ( on linux use date shell command). If yes, you just set the time and you're good.
If not you have to change it in php with setlocale() or date_default_timezone_set() function
It depends on your specific case, but I had saved the date as UTC / GMT time, so I had to use the gmdate function instead of date. E.g.
gmdate("H:i",$time)
I tried
$dtToday = DateTime::createFromFormat('Y-m-d', date('Y-m-d'));
but when I output it
die($dtToday->format('d M Y g:i:s a'));
I still get the time eg "22 Jan 2011 4:53:59 pm". Why is that?
UPDATE
Ah... many people misunderstood me, my bad, I forgot to point out the main point. I created the date with just the date portion, I don't want the time. So I'd expect something like
22 Jan 2011 12:00:00 am
You can call ->setTime(0, 0) to zero out the time portion:
$date = DateTime::createFromFormat('Y-m-d', '2011-01-22')->setTime(0, 0);
echo $date->format('d M Y g:i:s a');
// 22 Jan 2011 12:00:00 am
See the documentation for DateTime::createFromFormat:
If format does not contain the character ! then portions of the generated time which are not specified in format will be set to the current system time.
If you do the following function call, you'll get the result you expect:
$dtToday = DateTime::createFromFormat('!Y-m-d', date('Y-m-d'));
Today's start timestamp
$todayStartTS = strtotime(date('Y-m-d', time()) . ' 00:00:00');
You can do this by passing the current unix timestamp as the second parameter to the date function
echo date("Y-m-d H:i:s",time());
Remove this part g:i:s a from your code.
Now, if you want a nice date formatted according to your local, i recommand you to use strftime() function.
You are getting "22 Jan 2011 4:53:59 pm" because those are the rules you format your date with :
d (day) : 22
M (Month) : Jan
Y (Year) : 2011
g (12-hour format) : 4
i (minutes): 53
s (seconds): 59
a (am/pm): pm
Be more speciffic about the format would you like your timestamp to have.
I suggest you take a peak at the php date documentation.
Is it using UTC, or something?
I have a PHP version that gives me an error whenever I do something date related without first using date_default_timezone_set. Maybe that'll help you.