Change the ISO 8601 date format using PHP - php

I am trying to get the current date/time using Data Type: ISODateTime and Format: YYYY-MM-DDTHH:MM:SS e.g. 2012-02-06T08:35:30. I searched how I would do this in PHP and found that I can use;
$formatedDate = date("c");
Although the output of this is almost correct is not quite what I need and I can't figure out how to alter it, the current output of this is;
2014-07-01T10:53:10+02:00
My problem is I need to remove the "+02:00" and also this time is an hour ahead of my local time, which is what I need. Therefore, in this example, I would require;
2014-07-01T09:53:10
Any help would be really appreciated. Thanks.

You could just format the date manually.
$formattedDate = date('Y-m-d\TH:i:s');
To get your local time, use date_default_timezone_set() to set the appropriate timezone (before declaring $formattedDate).
// Change 'America/New_York' to your timezone
date_default_timezone_set('America/New_York');
See demo
To find your timezone, see the List of Supported Timezones.

Related

timestamp from both date & time inputs and compare php

I've been struggling to get an exact answer for this question. There are many that are close to what I'm wanting but seem to still be just off. The application of this is to ensure that a booking can't be made for a past date.
I have a form which has an input for time & another for date. Firstly, I wan't to take both of these inputs & convert them to a timestamp.
This code returns nothing
$time_date = sprintf("%s %s", $pDate, $pTime);
$objDate = DateTime::createFromFormat('H:ia d/m/Y', $time_date);
$stamp = $objDate->getTimestamp();
echo $stamp;
So I've have tried using something like this
$pDate = $_POST['pDate'];
$pTime = $_POST['pTime'];
$full_date = $pDate . ' ' . $pTime;
$timestamp = strtotime($full_date);
echo $timestamp;
But for some reason it is returning an incorrect timestamp. (i've been using an online converter) 02/06/2014 as date & 12:23am as time, is not 1401625380. This according to the converter is Sun, 01 Jun 2014 12:23:00 GMT.
Does someone have working code for returning a timestamp of both time & date inputs?
Secondly I want to compare this timestamp with a specified one & check to see if it is greater than. I've created a timestamp for my timezone with this
$date = new DateTime(null, new DateTimeZone('Pacific/Auckland'));
$cDate = $date->getTimestamp();
echo $cDate;
and will simply have an if statement which compares the two and echos the appropriate message.
I feel as though there are multiple question on here that are ALMOST what I'm wanting to achieve but I can't manage to get them working. Apologies for the near duplicate.
Note: I'm using ajax to post form data (if this could possibly interfere).
Your second code snipped is correct. Assuming it's in datetime format (Y-m-d H:i:s).
From php manual about strtotime():
Each parameter of this function uses the default time zone unless a time zone is specified in that parameter.
Check your PHP default time zone with date_default_timezone_get() function.
To compare two dates, be sure they both are in same time zones.
For datetime inputs I personally use jQuery UI timepicker addon.
you receiving the time and date in string format - so i don't believe the ajax can interfere.
as for your question:
first of all - find out what is the locale timezone of your server. you can do it by this function: date_default_timezone_get.
if the answer doesn't suit you - you can use its "sister": date_default_timezone_set, and change it to whatever value you need (like 'Pacific/Auckland' - see the documentation there). it is also recommended to return it to the original value after you finish your stuff.
i believe fixing your locale timezone will solve your issue.

Ignore BST with php strtotime() function

Is there a way for me to tell the strtotime() function not to change the time I give it into BST? i.e. if do
date('g.ia', strtotime("2014-06-25T19:30"))
I want to get 7:30pm, just as if I entered
date('g.ia', strtotime("2014-06-25T19:30"))
(The first one currently returns 6:30pm)
I'm aware I could just write a manual check for the day/month and add an hour if necessary, or just parse the time myself from the string, but both solutions sound a bit messy (I'll have to do this in quite a few places).
Sorry if there's something obvious I'm missing, pretty new to php
Function date() will format time based on your timezone setting. Said that, your example doesn't make sense since strtotime() will use current timezone setting to convert input to unix timestamp, and then function date() will use again that timezone setting to format timestamp back. You must be changing timezone setting between strtotime() and date() function calls, like this demo.
You can simply use DateTime extension, where you implicitly tell in what timezone is your time:
$dt = new DateTime('2014-06-25T19:30', new DateTimezone('Europe/London'));
echo $dt->format('g.ia');

how to change the default time zone in just one of my php file not config file? [duplicate]

This question already has an answer here:
Changing timezone in PHP
(1 answer)
Closed 9 years ago.
I have to change the time zone for one of my services; so just one file not in config for all php files! How can I do that! in that file I'm using filectime but apparently it is using another time zone!
Your help is appreciated.
If you need more clarification, please let me know
ADDED:
Sorry to say that but still after using date_default_timezone_set fro my city, my problem is not solved! I have a file the creation of which is : Nov 07 2013 7:36:50 but after using your time zone function for my city and following code:
$createTime = filectime($dirPath . '/' . $file);
the $creattime still it is : 2013-11-08T00:36:50Z
So it seems that time zone doe not have any effect on filectime! apart from adding hours (the difference hours) to the result of my filectime to adjust the difference, is there any other better ways?
Thanks
You might find php's date_default_timezone_set() helpful.
date_default_timezone_set() sets the default timezone used by all
date/time functions.
For example:
date_default_timezone_set('America/Los_Angeles');
Here is a list of supported timezones.
Edit:
I overlooked the fact that you're using filectime(). That function returns a unix timestamp, which does not contain any timezone data. So date_default_timezone_set() will not affect those results.
Instead, you can "translate" a date object from the server's timezone to a different timezone in PHP like this:
// for testing purposes, get the timestamp of __FILE__
// replace __FILE__ with the actual file you want to test
$time_created=filectime(__FILE__);
// build date object from original timestamp
$date=new DateTime(date('r',$time_created));
// translate the date object to a new timezone
date_timezone_set($date, timezone_open('America/Los_Angeles'));
// output the original and translated timestamps
echo"<p>Original Timestamp: ".date('r',$time_created)."</p>";
echo"<p>Los Angeles Timestamp: ".date_format($date,'r')."</p>";
Here's a working example.

How to format an UTC date to use the Z (Zulu) zone designator in php?

I need to display and handle UTC dates in the following format:
2013-06-28T22:15:00Z
As this format is part of the ISO8601 standard I have no trouble creating DateTime objects from strings like the one above. However I can't find a clean way (meaning no string manipulations like substr and replace, etc.) to present my DateTime object in the desired format. I tried to tweak the server and php datetime settings, with little success. I always get:
$date->format(DateTime::ISO8601); // gives 2013-06-28T22:15:00+00:00
Is there any date format or configuration setting that will give me the desired string? Or I'll have to append the 'Z' manually to a custom time format?
No, there is no special constant for the desired format. I would use:
$date->format('Y-m-d\TH:i:s\Z');
But you will have to make sure that the times you are using are really UTC to avoid interpretation errors in your application.
If you are using Carbon then the method is:
echo $dt->toIso8601ZuluString();
// 2019-02-01T03:45:27Z
In PHP 8 the format character p was added:
$timestamp = new DateTimeImmutable('2013-06-28T22:15:00Z');
echo $timestamp->format('Y-m-d\TH:i:sp');
// 2013-06-28T22:15:00Z
In order to get the UTC date in the desired format, you can use something like this:
gmdate('Y-m-d\TH:i:s\Z', $date->format('U'));
To do this with the object-oriented style date object you need to first set the timezone to UTC, and then output the date:
function dateTo8601Zulu(\DateTimeInterface $date):string {
return (clone $date)
->setTimezone(new \DateTimeZone('UTC'))
->format('Y-m-d\TH:i:s\Z');
}
Edit: clone object before changing timezone.
Since PHP 7.2 DateTimeInterface::ATOM was introduced in favor of DateTimeInterface::ISO8601, although it still lives on for backward compatability reasons.
Usage
$dateTimeObject->format(DateTimeInterface::ATOM)

Php Date Function

I have tried using php date function() like as follows
$date=date('Y-m-d').' '.date('H:i:s');
echo $date;
the output displayed is 2013-04-03 09:04:02.. but my system is 02:49 pm...
What time is being displayed for me? I tried changing the internet timing even then I am getting the same answer ?
First off, it is not necessary to use the date function twice. This will do the same thing:
echo date('Y-m-d H:i:s');
Second, you need to set PHP's date.timezone. This can be done in the php.ini file, but it can also be done using the date_default_timezone_set function, like this:
date_default_timezone_set('Europe/Amsterdam');
The string that you have to put in can be found in the documentation.
It may also be worth noting that you can tell the date function to use any time. This is done by passing in a *nix timestamp as the second argument. For example:
// One week ago from now
echo date('Y-m-d H:i:s', time()-604800);
It will show server's time only. If possible compare with your server time. If you want to use local machine's time you need to go with JAVASCRIPT.
And another suggestion,
You don't have to use individually to display date & time. You can achieve this in a single statement like this.
$date=date('Y-m-d H:i:s');
You will get the same format 2013-04-03 09:04:02
check for your system timezone and your default timezone in php by opening phpinfo()

Categories