How to format a PHP date in .Net DataContractJsonSerializer format? - php

There is a requirement to send a date inside a JSON post using PHP in this following format
\/Date(410256000000-0800)\/
How do I convert a standard dd-mm-yyyy h:i:s datetime like 01-01-2013 12:00:00 to that format in PHP? Just need to know what values correspond to what in that format, not really look for a stringify things answer.

This should do it:
$dateTime = DateTime::createFromFormat('d-m-Y H:i:s', '01-01-2013 12:00:00');
$requiredJsonFormat = sprintf(
'\/Date(%s%s)\/',
$dateTime->format('U') * 1000,
$dateTime->format('O')
);
echo $requiredJsonFormat; // prints '\/Date(1357038000000+0100)\/'
I leave it up to you to find what the formats U and O do from http://php.net/date.
An alternative would be to use PHP's DOTNET API and use the DataContractJsonSerializer class directly from PHP. However, you'd need .NET installed on the server and using PHP's DOTNET API is rather arcane.
The more interesting part is why you need this format at all. This is explained in a blogpost at http://weblogs.asp.net/bleroy/archive/2008/01/18/dates-and-json.aspx
But because of a strange oversight in the EcmaScript specs, there is no standard way of describing dates in JSON. […] Our current approach is using a small loophole in the JSON specs. In a JSON string literal, you may (or may not) escape some characters. Among those characters, weirdly enough, there is the slash character ('/'). […] The new format is "/Date(1198908717056)/" where the number is again the number of milliseconds since January 1st 1970 UTC […] The point is that this disambiguates a date literal from a string that looks like the same date literal, while remaining pure JSON that will be parsed by any standard JSON parser. Of course, a parser that doesn't know about this convention will just see a string, but parsers that do will be able to parse those as dates without a risk for false positives

We created a package for this: https://github.com/webapix/dot-net-json-date-formatter
It uses a similar approach than the previous answer, tested on PHP versions >= 5.6.
use DateTime;
use DateTimeZone;
use Webapix\DotNetJsonDate\Date;
$date = DateTime::createFromFormat(
'd-m-Y H:i:s',
'01-01-2013 12:00:00',
new DateTimeZone('+0000')
);
Date::toJsonDate($date); // returns '/Date(1357041600000+0000)/'

Related

How to separate the timezone and datetime from a string?

I have a string get from a text file name.
$string = "20181011000000GMT+0800"
// trying to convert it
Carbon::createFromFormat('YmdHisTO', $string)->format...
It shows me this error.
Unexpected data found. The timezone could not be found in the database
Data missing
How can i separate the datetime and timezone, if there's GMT+0800 end of the datetime string?
You can parse that format with a slight change to the format string you're using:
$string = "20181011000000GMT+0800";
Carbon::createFromFormat('YmdHisT+', $string);
The + sign tells the parser to ignore everything after the first timezone identifier (GMT). Strictly, it'll work without the + too, but I prefer to use it for clarity. Up to you though.
See https://3v4l.org/kDorn
Actually, testing this a bit more, I'm not sure the plus sign is that relevant. I think either of the timezone format characters (T and O) are simply swallowing all of the timezone information, rather than just their specific parts. DateTime's parser can be a bit of a minefield sometimes.
I would recommend using strtotime() it accepts every valid datetime format. See usage: http://nl1.php.net/manual/en/function.strtotime.php
$DateTimeString = "20181011000000GMT+0800";
$timestamp = strtotime($DateTimeString);
// trying to convert it
Carbon::createFromTimestamp($timestamp)->toDateTimeString();

RFC 3339 how make a dateTime from

I'm trying to format a date passed from a google plus Api thats like the guide says in RFC 3339 format:
PUBLISHED-> datetime-> The time at which this activity was initially published. Formatted as an RFC 3339 timestamp.
So by php documentation i found that:
DATE_RFC3339
Same as DATE_ATOM (since PHP 5.1.3)
And that both format are something like:
"Y-m-d\TH:i:sP"
Actually the output of the Google api is something like:
2014-01-22T10:36:00.222Z
When I'm trying to launch command like:
$date = DateTime::createFromFormat("Y-m-d\TH:i:sP", $activity['published']); //$activity['published'] contain the date
I have always FALSE as return.
In my opinion the problem is in the final part
.222Z
any suggestion will be appreciate before cutting it by some rudimental approach...
You don't need to use DateTime::createFromFormat() for standard inputs. Just use:
$date = new DateTime('2014-01-22T10:36:00.222Z');
var_dump($date);
But if you still insist to use createFromFormat(), then use correct format, with microseconds:
$date = DateTime::createFromFormat('Y-m-d\TH:i:s.uP', '2014-01-22T10:36:00.222Z');
var_dump($date);
There is a trick. A special constant DATE_RFC3339 was made to help, but it does not work if the last character is "Z" - which is perfectly fine for rfc3339 format. Actually JSON would specify format like that:
expected format YYYY-MM-DDThh:mm:ssZ or YYYY-MM-DDThh:mm:ss+hh:mm
But using this DATE_RFC3339 you can receive an Error message from PHP:
InvalidArgumentException: The timezone could not be found in the database
That is why we need to specify format manually:
With DateTime
$date = DateTime::createFromFormat ('Y-m-d\TH:i:s.u\Z', $time);
With Carbon:
\Carbon\Carbon::createFromFormat('Y-m-d\TH:i:s.u\Z', $time);

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)

CodeIgniter/JQuery/MySQL DateTime

Within my CodeIgniter app, I'm using a Jquery calendar pop-up that also captures time as set by the user, so the end result looks like: MM-DD-YYYY HH:MM, and I'm storing this in MySQL into a DateTime field that is: YYYY-MM-DD HH:MM:SS. What is the best (most efficient) way to push the date/time into MySQL so that it saves properly, and to pull is back out of MySQL and render it on the screen in the reverse format? Thanks!
Most efficient way is to use the ISO 8601 standard to pass date values between the client and server. Since the client and server talks in strings you'd be parsing the date to a string before sending it either way. The best format I prefer is the combined date and time in UTC:
2011-06-14T13:57Z
There are no spaces and it's clean. Then you'll have to parse it on the server side (should be relatively easy using PHP) and parse it on the client side.
For displaying purposes, I prefer to extend JavaScript's Date.prototype to include a format function that imitates PHP's date format.
Once you include the linked script from above you could do this on the server side -
var today = new Date();
alert(today.format('m-d-Y H:i')); //displays "06-14-2011 11:18"
Good luck!
I think you should use the strptime() function to parse the date received from the jQuery calendar your using and using mktime():
// Parse the time based on your jQuery calendar's format
$parts = strptime($calendar_value, '%m-%d-%Y %H:%M');
if ( ! empty($parts) )
{
// Create a Unix timestamp
$timestamp = mktime($parts['tm_hour'], $parts['tm_min'], 0, $parts['tm_mon'] + 1, $parts['tm_mday'], $parts['tm_year'] + 1900);
// Create a string representation of the Unix timestamp
$date = date(DATE_ISO8601, $timestamp);
}
You'll want to use $date to insert in your database. There is a function called "strtotime" which will attempts to parse dates that are in human-readable format but I doubt it's able to determine if the month or day comes first, especially if they're both lower than 12 which is why I chose to use "strptime" instead.
When you pull the data from MySQL, you can then simply use the date() and strtotime() function to populate the calendar:
echo date('m-d-Y h:i', strtotime($mysql_date));

what is the best method to translate strings to mysql dates?

What is the best way in php to take the following string mm[some char]dd[some char]yyyy and translate it to yyyymmdd?
I will probably want in the future, according to local do the same with dd[some char]mm[some char]yyyy.
If there is a way that already uses the Zend Framework API, the better
<?php
$str = '08-24-1989'; // can be in any recognizable date format.
$new_str = date('Ymd', strtotime($str)); // produces "20090824".
?>
You can replace Ymd in the second statement above with any date format characters found here.
If you're looking to use Zend's Zend_Date framework, check out some examples and documentation here. Quite frankly though, the PHP functions are a lot simpler and easier to use in your case.
date('Ymd', strtotime($time));
Strtotime is absolutely the best tool to translate almost any time format into a standard one that you can then use Date to put into the format you want.
Because you question title says MySQL Dates, this is the string format that mysql uses.
date('Y-m-d h:i:s', strtotime($time));
Unless [some char] varies , use the mysql str_to_date function, e.g. STR_TO_DATE('12|23|2009','%m|%d|%Y');
I would absolutely use TIMESTAMP for any date storage. It's incredibly easy to handle time differences (like SELECT ... WHERE date BETWEEN 2138728753 AND 376251237) and can be translated to any locale pretty easily :)

Categories