I have a user input in web-form in HTML5 type="date" field. Then this entered value (together with others) is saved as JSON document in MySQL database. Now I need to extract that date and print it into generated MS Word document with one given format. Great! But as you know HTML5 date field accepts date format depending on user locales, settings, etc. I noticed that all dates entered in different browsers are saved in DB in "Y-m-d" format. Is this really so in all the cases? Can I rely on this format for future usage? Could not find any specification information on the topic.
If one can not rely on "Y-m-d" format are there any PHP libraries for "guessing" date format?
First of all, why are you saving JSON document in MySQL database instead of some more suitable document database (e.g. MongoDB)?
Anyway, based on this and this the displayed date format differs from the actual value. The date value in the HTTP Request will be always formatted as yyyy-mm-dd, which is the ISO 8601 format and I guess it should be reliable.
In PHP you can process date strings with the DateTime class. This class supports many different date formats. For example:
// timezone is set based on your system preferences if not set
$date1 = new DateTime('2017-04-27', new DateTimeZone('+00:00'));
$date2 = new DateTime('27 April 2017', new DateTimeZone('+00:00'));
$date3 = new DateTime('2017-04-27T00:00:00+00:00');
var_dump($date1 == $date2); // bool(true)
var_dump($date1 == $date3); // bool(true)
If the date string is wrong, an exception is thrown:
try {
$date = new DateTime('2017 4 27');
} catch (Exception $e) {
echo $e->getMessage(); // DateTime::__construct(): Failed to parse time string (2017 4 27) at position 5 (4): Unexpected character
}
So, in your case you can you this class for date validation and than proper output formatting, e.g.:
$dateString = '2017-04-27T14:22:11+00:00';
try {
$date = new DateTime($dateString);
echo $date->format('Y-m-d'); // 2017-04-27
} catch (Exception $e) {
// do something else
}
Related
I have a Laravel-based app that is used by people from various parts of the US.
I am capturing a timestamp in Javascript when the user takes a specific action, and then I am submitting that timestamp as form data, for the Laravel/PHP to process.
The timestamp that I am capture in Javascript is in typical "YYYY-MM-DD HH:MM:SS" format.
I have the timezone the user is in stored in a database.
I basically want to take that timestamp, and convert it to UTC time, so that all timestamps in the database are UTC.
That is where I am struggling.
I have the following PHP code:
$defaultTime = request('submitted-time-stamp'); //In this case, we'll say 2022-12-21 12:01:01
$defaultTZ = $user->time_zone; //Translates to America/Denver
$utcTime = new DateTime($defaultTime);
$convertedTime = $utcTime1->setTimeZone(new DateTimeZone('UTC'));
$formattedTime = $convertedTime->format("Y-m-d H:i:s");
echo $formattedTime;
This code – it isn't producing any errors per sé... but it is showing the wrong time. It's showing the time that it went in as, not the time converted to UTC.
Basically, if I submit "2022-12-21 12:01:01" as the time, the converted time SHOULD be "2022-12-21 19:01:01", but it's still just echoing out "2022-12-21 12:01:01".
What am I missing here?
setTimezone() changes the timezone of the object from whatever default it was created with. I.e., it means, "convert from the existing timezone to this new timezone." It does not mean, "interpret the time as if it were in this timezone." If the original string didn't contain some sort of timezone identifier, then that default is whatever your PHP config says.
$when = new DateTime('2022-12-21 12:01:01');
echo $when->getTimeZone()->getName();
This will be the same as:
echo date_default_timezone_get();
Which is probably not what you want unless all your users are in the same timezone as your server.
In order to create a DateTime object in a specific known timezone that is not the same as your server's default, you'll need one of two things -- either a timezone representation in the input string:
$when = new DateTime('2022-12-21 12:01:01 America/New_York');
Or an explicit default timezone passed as a second parameter to the DateTime constructor:
$userDefaultTzStr = 'America/New_York'; // read this value from the database
$defaultTz = new DateTimeZone($userDefaultTzStr);
$when = new DateTime('2022-12-21 12:01:01', $defaultTz);
This latter method is (probably) preferred. If the input string contains any sort of timezone identifier, that will be used and the second parameter will be ignored. But if the input string does not contain any sort of timezone identifier, then the string will be interpreted as if it were in the indicated timezone.
Using Carbon it's very trivial.
use Carbon\Carbon;
$date = Carbon::create(request('submitted-time-stamp'), $user->time_zone);
$date->tz('UTC');
echo $date->format('Y-m-d H:i:s');
It should be the same thing with Laravel's Date facade.
use Illuminate\Support\Facades\Date;
$date = Date::create(request('submitted-time-stamp'), $user->time_zone);
$date->tz('UTC');
echo $date->format('Y-m-d H:i:s');
I need to hit an API in PHP Laravel and passing Raw body date in it. It requires DataTime to be sent as required by json show in below format.
"ShippingDateTime": "\/Date(1484085970000-0500)\/",
How can I create date like this in PHP/Laravel where I can get any future date (current date + 1). Currently it's giving error that:
DateTime content '23-01-2021' does not start with '\/Date(' and end with ')\/' as required for JSON.
It looks like you have a Unix timestamp with milliseconds (the 000) on the end, plus a timezone identifier. You should be able to construct that with the date formatting flags UvO (unix time, milliseconds, timezone)
(These are in my timezone, -06:00)
echo date('UvO');
// 1611339488000-0600
// Surround it with the /Date()/ it requests
// Encode it as JSON wherever is appropriate in your code
echo json_encode('/Date(' . date('UvO') . ')/');
// "\/Date(1611339460000-0600)\/"
Assuming you have your dates in DateTime objects, call their format() method to produce your desired date format.
// create your DateTime as appropriate in your application
$yourdate = new \DateTime();
echo json_encode('/Date(' . $yourdate->format('UvO') . ')/');
// Set it ahead 1 day in the future
$yourdate->modify('+1 day');
I am trying to make user-input accept a date to limit searching. I use the following:
try {
$begin = new \DateTime($range['begin']);
} catch (\Exception $e) {
$begin = null;
}
If the date fails to construct, then the query (not shown) is not modified.
Occasionally the users enter just a month, and it would be obvious then that the user wants to search between the first and last day of the month, and I might modify the query in a different way.
Unfortunately php infers, without other specification that the user means the first of the month. Is there any way to determine if this has happened? Another way to ask this question is how does one determine that a user has supplied a valid month without a day and distinguish that from supplying a date with first day of the month that leverages the myriad formats DateTime automatically interprets?
My first pass used date_parse, but that also infers the date.
better always to know format of input value, so then you can simple convert to datetime object, for example:
$inputFieldValue = '22/02/2017';
$date = \DateTime::createFromFormat('d/m/Y', $inputFieldValue);
if ($date instanceof \DateTime) {
echo 'input is valid date, do what you need';
} else {
echo 'input is not valid date';
}
more documentation http://php.net/manual/en/datetime.createfromformat.php
I'm creating a forum, which also stores the time a post was sent, and I need to convert that into the user's timezone.
Now, the MySQL DataBase stores the time with UTC_TIMESTAMP() (in a column with the DATETIME type), and I created a little function from the code on http://www.ultramegatech.com/blog/2009/04/working-with-time-zones-in-php/ to convert the time to the user's timezone. This is the function:
function convertTZ($timestamp, $tz='UTC', $format='d-m-Y H:i:s') {
// timestamp to convert
$timestamp = strtotime($timestamp);
// the time formatting to use
$format = $format;
// the time zone provided
$tz = $tz;
// create the DateTimeZone object for later
$dtzone = new DateTimeZone($tz);
// first convert the timestamp into an RFC 2822 formatted date
$time = date('r', $timestamp);
// now create the DateTime object for this time
$dtime = new DateTime($time);
// convert this to the user's timezone using the DateTimeZone object
$dtime->setTimeZone($dtzone);
// print the time using your preferred format
$time = $dtime->format($format);
return $time;
}
And I made a test page at http://assets.momo40k.ch/timezones.php.
Now, when I insert a post into the DataBase at, say, 11:50 in my timezone (which is Europe/Rome), it inserts 09:50 in UTC, wich is correct, according to some online timezone converters.
But when I try to convert it back to Europe/Rome with the convertTZ() function, it returns 09:50, as if Europe/Rome is UTC. If I try converting it to a GMT+2:00 timezone, it returns 10:50. Can anyone fugure out why this is?
P.S: I'm not using the CONVERT_TZ() SQL function because my server does not support named timezones, so this function is my workaround.
Make sure your stored timestamps are UTC:
$date = new DateTime($timestamp, new DateTimeZone("UTC"));
$date->format(DATE_W3C); // does it gives the expected result ?
BTW your function can be simplified to this:
function convertTZ($timestamp, $tz='UTC', $format='d-m-Y H:i:s') {
$dtime = new DateTime($timestamp, new DateTimeZone("UTC"))
$dtime->setTimezone(new DateTimeZone("UTC"));
return $dtime->format($format);
}
MySQL always stores TIMESTAMP fields in UTC internally (that's the definition of a unix timestamp, in fact). So when you SELECT or UPDATE/INSERT/REPLACE, the time you get or set is always in the MySQL server's local time zone.
So a common mistake is to store UTC_TIMESTAMP(), which MySQL interprets as a local time and so the current time gets double-converted to UTC when it stores it internally in the field as a unix TIMESTAMP.
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));