Converting between timezones in PHP - php

I am converting this time and date:
Thu, 31 Mar 2011 02:05:59 GMT
To the following time and date format:
Monday March 28 2011 4:48:02 PM
I am using the following PHP code to accomplish this, but I want to convert all time zones to PST/PDT. I looked at the PHP manual and saw this date_default_timezone_set() but I am not sure how to implement that into the code I have below.
$date = $messages[0]->CreationTime;
echo date('l F j Y g:i:s A I', strtotime($date))

I would not use date_default_timezone_set for general TZ conversions. (To clarify... if this is for display purposes, script wide, then using the default timezone is a reasonable thing to do.)
Instead, I would use something like:
$tz = new DateTimeZone('America/Los_Angeles');
$date = new DateTime('Thu, 31 Mar 2011 02:05:59 GMT');
$date->setTimezone($tz);
echo $date->format('l F j Y g:i:s A I')."\n";

$date = $messages[0]->CreationTime;
date_default_timezone_set('America/Los_Angeles');
echo date('l F j Y g:i:s A I', strtotime($date));
See this list for available timezones that get passed into the function

Related

How fix bad date in carbon now() with ->isoFormat(' d MMM Y ')

i have problem i laravel with carbon.
Carbon::now() works ok, but i need use this code ->isoFormat(' d MMM Y ') for convert date to much user friendly format "6 nov 2019".
But after convert is return value with different date (day).
WHY? what is bad?
update:
and any idea how i can use it in multilanguage sites in laravel project?
example: user EN have 6 nov 2019 user CS have 6 lis 2019 //czech
->isoFormat(' d MMM Y ') // have bad date, but in cs write "6 lis 2019" ->format('d M Y') // have good date, but in cs lang use english "6 nov 2019"
<?php
use Carbon\Carbon;
$datetime = Carbon::now();
$newdate = $datetime->isoFormat(' d MMM Y ');
echo $datetime; // show 2019-11-06 13:35:49
echo $newdate; // show 3 nov 2019
?>
How about $newdate = $datetime->format('d M Y');
Output is 06 Nov 2019
Carbon is just a class extending DateTime.
Use DateTime! https://www.php.net/manual/en/class.datetime.php
when you echo $datetime, there is obviously a __toString() function in Carbon, which by the look of it defaults to the standard Y-m-d H:i:s format.
Nothing looks wrong except your format.
<?php
$datetime = new DateTime();
$newdate = $datetime->format(' d M Y ');
echo $datetime->format('Y-m-d H:i:s'); // shows 2019-11-06 13:35:49
echo $newdate; // shows 6 Nov 2019
You can also use carbon Carbon::createFromFormat as below:
$formated_date = Carbon::createFromFormat('d M Y', Carbon::now());
echo $formated_date;

Convert a datetime to another format using php

I am trying to convert the following datetime string into another format of date & time.
Thu Dec 15 10:05:14 +0000 2016
To
15 Dec
My code is
created_at = 'Thu Dec 15 10:05:14 +0000 2016';
$new_dt = date_format(strtotime($created_at),'jS F');
But it displays nothing.Any suggestion is highly appreciated.
date_format() is an alias for DateTime::format(), and it accepts DateTimeInterface instances (e.g. DateTime objects), not raw UNIX timestamps.
Also, relying on "magic" functions like strtotime() is bad in general.
What you need is the following:
$new_dt = DateTime::createFromFormat('D M d H:i:s O Y', $created_at)
->format('jS F');
Or if you really insist on using the procedural-style functions:
$new_dt = date_format(
date_create_from_format('D M d H:i:s O Y', $created_at),
'jS F'
);

how can I format my date in php to have the query new Date() format?

I have a php script that returns the date of the server:
<?php
echo date('D, d M y H:i:s a');
?>
but when I print this value on the client site I get:
Tue, 29 Sep 15 16:19:28 pm
But instead I need the date in this format:
Tue Sep 29 2015 16:18:00 GMT+0200 (Central Europe Daylight Time)
How should I modify my php script then to have it like this?
Thanks!
echo (new DateTime())->format('r');
$datestring = "26-08-2015 03:35:28"; //date as string
$date = new DateTime($datestring); //String to datetime conversion
$date = $date->format('D d M y H:i:s O e'); //format the date
echo $date;
This is the manual link. Timezone you have to set.
Output will look like Wed 26 Aug 15 03:35:28 +0200 Europe/Paris
Go to the very bottom of this page http://php.net/manual/en/function.date-default-timezone-set.php

gmmktime and mktime give me the same number

My PHP timezone is 'Asia/Tehran' so I would not expect gmmktime give me the same result as mktime.
php info:
Default timezone Asia/Tehran
However when I type in my php console:
echo date('jS F Y h:i:s A (T)', mktime());
3rd September 2013 11:23:10 AM (IRDT)
echo date('jS F Y h:i:s A (T)', gmmktime());
3rd September 2013 11:23:16 AM (IRDT)
While I expect 3.5 or 4.5 hours time difference. Am I making a mistake somewhere?
you need to give Argumente inside of gmmktime(). Your mistake is, understand first mktime() vs gmmktime(). see this link.
And The workaround is simple, use gmdate() function to display dates created with gmmktime().
<?php
$inputDate = gmmktime(0,0,0,2,7,2012);
echo gmdate("M d Y H:i:s O", $inputDate);
// Feb 07 2012 00:00:00 +0000
?>

Timestamp to normal date format and reverse conversion using PHP

I have a timestamp as 2011-08-27 18:29:31. I want to convert it to 27 Aug 2011 06.29.31 PM. Also, I want to convert this format reverse back to the previous timestamp format.
How van I do this using PHP?
$converted = date('d M Y h.i.s A', strtotime('2011-08-27 18:29:31'));
$reversed = date('Y-m-d H.i.s', strtotime($converted));
Don't use date()! It`s too old function. In PHP v. 5.2 and more you should use date_format or DateTime::format object.
you can use the date_format() function
//Convert to format: 27 Aug 2011 06.29.31 PM
$converted_date = date_format('d M Y h.i.s A',strtotime($orig_date));
//Convert to format 2011-08-27 18:29:31
$converted_date = date_format('Y-m-d H:i:s',strtotime($orig_date));
Have you looked at the date functions in PHP, especially strftime (for formatting a timestamp) and strtotime
To convert from 2011-08-27 18:29:31 to 27 Aug 2011 06.29.31 PM:
echo date('d M Y, H.i.s A', strtotime('2011-08-27 18:29:31'));
To do the reverse:
echo date('Y-m-d H:i:s',strtotime('27 Aug 2011 06.29.31 PM'));
If that doesn't work, you may have to try:
$date = date_create_from_format('d M Y, H.i.s A', '27 Aug 2011 06.29.31 PM');
echo date_format("Y-m-d H:i:s",$date);

Categories