Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datetime-local HTML5 #356

Closed
alexbowers-tecmark opened this issue May 6, 2015 · 17 comments
Closed

datetime-local HTML5 #356

alexbowers-tecmark opened this issue May 6, 2015 · 17 comments

Comments

@alexbowers-tecmark
Copy link

HTML5 datetime-local input field requires the format to be:

  • A date.
  • The literal string "T".
  • A time.

This can be done with formatting, but having a ->toDatetimelocal() would be useful.

I couldnt find anything that already does this in the documentation.

http://www.w3.org/TR/html-markup/input.datetime-local.html

@briannesbitt
Copy link
Owner

See : http://carbon.nesbot.com/docs/#api-commonformats

echo $dt->toW3cString(); // 1975-12-25T14:15:16-05:00

@alexbowers-tecmark
Copy link
Author

Thanks

@tevfik6
Copy link

tevfik6 commented Jul 27, 2016

Hi @briannesbitt,

It seems like this is not working anymore. I'm getting warning from Chrome Version 51.0.2704.103 (64-bit) and input[type="datetime-local"] doesn't show anything. It looks like it can't even parse this.

The specified value "2016-07-28T01:02:00+00:00" does not conform to the required format. The format is "yyyy-MM-ddThh:mm" followed by optional ":ss" or ":ss.SSS".

Tev.

@bjuppa
Copy link

bjuppa commented Aug 2, 2016

Hi!
It could make sense to reopen this request! The HTML5 datetime input is happy with the toW3cString() format, but datetime-local requires another similar format, up to but completely without the timezone info.

Current workaround is to use $dt->format('Y-m-d\TH:i:s')
Could toDatetimelocalString() be a suitable name to match the others in http://carbon.nesbot.com/docs/#api-commonformats?

@papalardo
Copy link

toDateTimeLocalString() works to me

@bjuppa
Copy link

bjuppa commented Sep 27, 2019

Until just now I wasn't even aware there had been a PR for this suggestion merged over a year ago! I'm so glad to see it in there, thanks to @yamenarahman!

@yamenarahman
Copy link
Contributor

You’re welcome.

@weshooper
Copy link

A specific format for this would still be useful.

While toDatetimelocalString() does work, Chrome/Brave pickup the seconds and confusingly render them:

Screenshot 2019-10-15 at 15 54 19

A format without the seconds (i.e. a shortcut for ->format('Y-m-d\TH:i') would be handy, since it'd render as:

Screenshot 2019-10-15 at 15 52 59

@kylekatarnls
Copy link
Collaborator

Macro?

Carbon::macro('pickerFormat', static function () {
  return self::this()->format('Y-m-d\TH:i');
});

echo Carbon::now()->pickerFormat();
// or statically: echo Carbon::pickerFormat();

@bjuppa
Copy link

bjuppa commented Oct 15, 2019

That's interesting @weshooper!

I also see now that all the examples on the MDN input docs are without the seconds part, and it actually says "Seconds are not supported". But the MDN date and time formats docs says seconds are valid, and so does Whatwg.

If real-world datetime-local inputs works better without the seconds part, I think toDateTimeLocalString() should drop seconds and format using Y-m-d\TH:i!

I guess it's a breaking change though 😄

@kylekatarnls
Copy link
Collaborator

kylekatarnls commented Oct 15, 2019

After a check I disagree this statement:

Chrome/Brave pickup the seconds and confusingly render

It's confusing only if you do not expect seconds as user input. But applications may require their user to input seconds and even milliseconds. This works fine with Chrome:

100m run start:
<input type="datetime-local" value="2018-06-12T19:25:12.324">
<br>
end:
<input type="datetime-local" value="2018-06-12T19:25:21.904">

Seconds matter (and even milliseconds could). So the user should be able to use them. And it should be explicitly removed if you don't want them.

For now it requires ->startOfMinute() call (the more explicit way).

But the improvement can be:

$date->toDatetimelocalString('minute');
$date->toDatetimelocalString('second');
$date->toDatetimelocalString('millisecond');
$date->toDatetimelocalString('microsecond');

But the default behavior would still be "second". I see no good reason to change it.

@bjuppa
Copy link

bjuppa commented Oct 15, 2019

@kylekatarnls I like that improvement suggestion, letting the consumer decide what level of detail is desired! Also, not a breaking change anymore! 😎

@weshooper
Copy link

weshooper commented Oct 15, 2019

I should probably add that, by 'confusing', I mean that while Chrome/Brave renders the seconds and milliseconds, they're displayed in a faded/disabled colour and aren't selectable or editable by the user, so can't be input.

@kylekatarnls
Copy link
Collaborator

kylekatarnls commented Oct 15, 2019

It can if you change the option step="1" (with a value lower than 60).

For datetime-local inputs, the value of step is given in seconds, with a scaling factor of 1000 (since the underlying numeric value is in milliseconds). The default value of step is 60, indicating 60 seconds (or 1 minute, or 60,000 milliseconds).

Source: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime-local

@weshooper
Copy link

@kylekatarnls Aha, did not realise that - thanks! :-)

kylekatarnls added a commit to kylekatarnls/Carbon that referenced this issue Oct 15, 2019
kylekatarnls added a commit that referenced this issue Oct 16, 2019
…me-precision

#356 Add toDateTimeLocalString precision parameter
@bveasey
Copy link

bveasey commented Dec 24, 2019

while this works for chrome using datetime-local on ios safari the seconds cause an error. Just noting this.

jpmaga added a commit to jpmaga/Carbon that referenced this issue Jan 5, 2020
* Unbind $this in macros only for PHP < 8

* Privatize loadMixin* method as reserved for mixin() method

* Temporary disabling binding $this/static for PHP 8

* Set specific tests for each macros in PHP 7 and 8

* Persian language localization fixes

* Update Persian translations unit tests

* Update uz_Arab and mzn translations unit tests

* Fix briannesbitt#1816 from now translation in Chinese

* Update zh tests

* Add explicit imports and update composer description

* Cleanup PHPDoc

* Cleanup PHPDoc

* briannesbitt#1782 Fix from_now translation in Chinese

* Add comparison methods for CarbonPeriod bounds

* Add PHPDoc comments

* Allow string and interval in floor, round and ceil

* Implement carbonize() method get a Carbon date from interval/period/any

* Add rounding methods to CarbonInterval/CarbonPeriod

* Fix PHPDoc and add tests

* Fix PHPDoc for Carbon rounding methods

* Fix self references

* Add tests

* Add cast() method and traits mixins to CarbonInterval and CarbonPeriod

* Allow static methods in mixin traits

* Update CarbonInterface PHPDoc

* briannesbitt#1822 Use less short variants for short units in Polish

* Make zh_HK and zh_TW extend Hant/Hans

* Implement contains() and JsonSerializable in CarbonPeriod

* Implement new methods in CarbonPeriod
  - getIncludedStartDate()
  - getIncludedEndDate()
  - follows()
  - isFollowedBy()
  - isConsecutiveWith()

* Simplify duplicate code

* [Travis-CI] Simplify xdebug settings

* Mute deprecation notice

* Ignore mutability in equalTo comparison of periods

* Fix unit tests

* Fix typo

* Fix getIncludedEndDate

* Improve any native PHP classes and sub-classes as cast

* Fix briannesbitt#1831 Refresh global translator caching

* Add cast tests

* Take recurrences DatePeriod incrementation into account

* Mute type wrong PHPStan detections in tests

* Fix briannesbitt#1834 language detection in Lumen

* Make zh_Hans inherit from zh_Hant

* Add altNumbers option to forHumans/diffForHumans

* Fix alternate numbers in hours

* Ensure consistent output typing

* Add tests for altNumbers option briannesbitt#1817

* Fix briannesbitt#1837 fixed from_now translation in Fnnish and add few seconds

* briannesbitt#1837 Update Finnish tests

* Fix briannesbitt#1840 add short form or year for year in Turkish

* Update Turkish unit tests

* Fix PHP 8 compatibility

* Add doc for setDate()

* briannesbitt#1837 Add translation author

* Fix setISODate PHP 8 signature compatibility

* Implement methods that change of signature in PHP 8

* Allow null in __set_state for compatibility with PHP 8

Possible error to fix in PHP:
php/php-src#4526

* Remove temporary fix as fixed in PHP itself:
php/php-src@b17957e

* Remove obsolete tests

* Allow PHP 8 tests to fail until new PHP build is released

* Add test for setISODate()

* Fix Chinese Hant/Hans and meridiem

* [AR] Update with enhancements

* More enhancements

* Update files and fixing test unit

* Fix StyleCI issues

* Fix briannesbitt#1851 Update all Carbon classes on Laravel locale change

* Remove irrelevant test

* Fix PHP 8 compatibility

* Use require everywhere instead of include

* Implement briannesbitt#1856 Allow strings in isSame* methods

* Update PHPDoc and add tests

* Fix 1860 Allow Closure in setTestNow()

* Add create() test in Carbon::setTestNow()

* Fix briannesbitt#1861 Add aUnit option in Thai

* briannesbitt#1861 Add @nithisa authorship

* Allow to control timezone in testNow Closure

* Add PHPDoc

* Synchronize CarbonPeriod and CarbonInterval with Laravel locale

Thanks to @craig-sen to point out this

* Simplify swap expressions

* Reset language for all classes before running Laravel locale test

* Improve period/interval string detection in Carbon::make()

This allows to use make() with customized parse() methods

* Fix briannesbitt#1842 Latvian declension, aUnit and short units

* Add previous author complete name

* Fix zh locales merging

* Remove irrelevant zh_MO overrides

* Corrected sl-SI

* briannesbitt#1828 Make spaces around numbers consistent (both sides or none)

* briannesbitt#1824 Fix interpolations precedence and ordering

* Add missing tests

* Fix code style

* Update Slovenian unit tests

* Apply ago/from_now/before/after declension in multi-parts diffs

* briannesbitt#1849 Add Slovenian short units and "from now" declension

* briannesbitt#1800 Fix Lithuanian declensions

* briannesbitt#1800 Update Lithuanian tests

* briannesbitt#1800 Add Lithuanian calendar and ordinal numbers translations

* briannesbitt#1800 Fix Lithuanian year nominative 11+ plural

* briannesbitt#1800 Decline after translations like from_now ones

* briannesbitt#1800 Fix units plural

* briannesbitt#1857 Fix static methods typing autocompletion

* Add recurrences support in Carbon::toPeriod()

* Fix code style

* briannesbitt#1814 Remove error muter

* Fix briannesbitt#1877 Support timezone short offset format

* Nit: Grammar (briannesbitt#1880)

https://writingexplained.org/a-vs-an-difference

* Use static notation for called class

* Allow to get context from macros using self::this()

* Add missing import

* Remove typehint specificity

* Fix isBefore documentation

There was an error in the documentation of the function isBefore.

2018-07-25 12:45:16 is before 2018-07-25 12:45:18.

* Fix briannesbitt#1886 Support slashes in hasFormat()

* briannesbitt#1886 Add hasFormat() tests

* briannesbitt#1889 Fix Belarusian units translations

* briannesbitt#1893 Fix hour translation to година

* briannesbitt#1889 Fix Belarusian aUnit option translations

* briannesbitt#1893 Fix hour Ukrainian units translations

* briannesbitt#1889 Fix Belarusian aUnit option translations tests

* Add translation authors

* Fix briannesbitt#1902 Make hr_BA inherit hr language

* Replace $this with CarbonInterface in PHPDoc (briannesbitt#1904)

* Replace $this with CarbonInterface in PHPDoc
* [PHPStan] Ignore Carbon interface to instances mismatch

* Fix briannesbitt#1910 seconds Serbo-Croatian translation (briannesbitt#1911)

* Fix briannesbitt#1910 seconds Serbo-Croatian translation

* briannesbitt#356 Add toDateTimeLocalString precision parameter

* Add documentation for getTimeFormatByPrecision

* Add $unitPrecision parameters to relevant methods

Add $unitPrecision parameters to:
- toTimeString
- toDateTimeString
- toIso8601ZuluString

* Update PHPDoc

* Fixed all greater examples

Fixed bad one + changed date to have at least 1 true example

* Fix comparison examples

* Improve typings

* Apply fixes from StyleCI (briannesbitt#15)

* Fix typings

* Fix typehint for static create methods (Fix briannesbitt#1914)

* added new CreateFromFormat method to CarbonInterval

* fixed CarbonInterval::createFromFormat() dockblock

added example
fixed return type

* added CarbonInterval::createFromFormat() tests

* fixed tests inputs

* fixed tests testDefaults() and testNulls()

* Add diff translate

* Add option for rounding in forHumans

* Fix code style

* Implement custom interval formats parsing

* Fix too short variable name

* Use customizable factors for forHumans() rounding

* Add test for gap

* Use dedicated method to get the round factor

* Fix briannesbitt#1927 Finnish future interval translations

* Add rounding methods

* Implement FLOOR and CEIL options

* Fix code style

* Remove unused variable creation

* Remove unused method getRoundFactor

* Fix briannesbitt#1929 Indonesian from now translation

* Allow Symfony 5

* Show difference between FLOOR option and no option

* Fix briannesbitt#1908 Korean spacing and aUnit option

* Add 2.x-dev branch alias

* Added diff before_yesterday and after_tomorrow

* briannesbitt#398 Skip tests with easter_days() if ext-calendar is missing

* [Travis-CI] Remove unneeded dev dependency from the install

* Fix briannesbitt#1941 Symfony 5 compatibility

* Fix briannesbitt#1941 Replace Symfony custom rules with inline syntax

* briannesbitt#1941 Set me and scr locales as aliases

* Compare locales handling aliases

* Fix briannesbitt#1943 Add example for H:s.vi (briannesbitt#1944)

* Fix briannesbitt#1943 Add example for H:s.vi

* Fix briannesbitt#1943 Drop support of .v for PHP < 7.3

* Fix test PHP < 7.2 compatibility

* Add test for unit accumulation in interval rounding

* Fix briannesbitt#1949 Use format('T') to match ISO 8601 timezone format (briannesbitt#1950)

* Fix briannesbitt#1951 createFromTimestamp in DST chnge

* briannesbitt#1951 Add test for createFromTimestampMs

* [Travis-CI] Switch to PHP 7.4 stable release

* briannesbitt#1871 Improve translations using CLDR database (briannesbitt#1955)

* Fix briannesbitt#1953 Align Korean translation on CLDR (briannesbitt#1956)

* Fix briannesbitt#1954 Improvement from-now Armenian translation (briannesbitt#1957)

* Fix briannesbitt#1958 Standardize integer timezone input

* Fix briannesbitt#1958 Remove timezone auto-detection

* Fix PHPDoc alignment

* Add timezone unit tests

* Ease tests with OS locales dependency

* Fix briannesbitt#1968 Turkmen year and short days translations

* Added briannesbitt#1966 Tamil before and after translations

* Update Teso unit tests files

* Use CarbonInterface instead of self for Date trait constant as keys

* Speed up resetMessages

* Update PHPMD

* Add strict restrictions for translator requirements

* Fix Symfony 3 compatibility

* Fix month overflow sensibility of quarter tests

* Use common method for translators able to getLocale()

* Add unit tests for NotLocaleAwareException

* Fix method signature compatibility

* Use include instead of require to not stop the process if lang file is missing

* Fix briannesbitt#1970 Uzbek before/after translations

* Cleanup imports

* Fix briannesbitt#1977 Greek "ago" translation

Co-authored-by: Kyle <kylekatarnls@users.noreply.github.com>
Co-authored-by: Hassan Pezeshk <hpez58@gmail.com>
Co-authored-by: Mohamed Sabil <me@mohamedsabil83.com>
Co-authored-by: Gal Jakič <gal.jakic@gmail.com>
Co-authored-by: Arthur Cinader <700572+acinader@users.noreply.github.com>
Co-authored-by: Marc-Antoine Bouchard Marceau <marcantoine.bouchardm@gmail.com>
Co-authored-by: karlyn33 <karlyn33@users.noreply.github.com>
Co-authored-by: spaceemotion <spaceemotion@users.noreply.github.com>
Co-authored-by: silvio-gratani <42873063+silvio-gratani@users.noreply.github.com>
Co-authored-by: dima-bzz <dimabzz@gmail.com>
Co-authored-by: Tyler Collier <github@tylercollier.com>
Co-authored-by: Graham Campbell <GrahamCampbell@users.noreply.github.com>
@Reza1290
Copy link

This how u use it in Laravel Blade Template

$var_date = "2024-01-25 11:04:00";

in blade
{{\Carbon\Carbon::parse($var_date)->toDateTimeLocalString()}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants