Skip to content

Instantly share code, notes, and snippets.

var dateString = '2018-01-25'; // can be numbers concat: year + '-' + month + '-' + day
var timeString = '12:30';
var date = new Date(dateString + 'T' + timeString); // use the `T` separator for cross-browser compatibility
var lang = 'fr';
var timezone = 'Europe/Paris'; // Use timezone = null to use the browser timezone
moment.prototype.userFormat = function (format) {
if (timezone) {
this.tz(timezone);
}
if (lang) {
this.locale(lang);
<?php
$date = $this
->tz($timezone ?: 'America/Chicago')
->locale($lang ?: 'en_US');
<?php
// Create a macro with user settings
CarbonImmutable::macro('userFormat', function ($format = null) use ($timezone, $lang) {
$date = $this->tz($timezone)->locale($lang);
if ($format) {
return $date->isoFormat($format); // translated formatting if custom format given
}
return $date->calendar(); // default to calendar without format
});
moment('2018-05-12T23:16:46.123456Z').locale('fr').calendar()
<?php
$utcString = '2018-05-12 23:16:46.123456';
$date = new CarbonImmutable($utcString);
echo $date->tz($timezone)->locale($lang)->calendar();
<?php
$inputDateString = '2018-05-12 23:16:46.123456';
$start = new CarbonImmutable($inputDateString);
$end = $start->modify('+2 days');
echo json_encode([
'title' => 'Foobar',
'start' => $start,
'end' => $end,
]);
<?php
$inputDateString = '2018-05-12 23:16:46.123456';
$date = new CarbonImmutable($inputDateString);
echo $date->toJSON();
<?php
setlocale(LC_TIME, "$lang.UTF-8", "$lang.utf8", $lang, strstr($lang, '_', true));
$utcString = '2018-05-12 23:16:46.123456';
$date = new DateTimeImmutable($utcString);
$localString = $date->setTimezone(new DateTimeZone($timezone))->format('Y-m-d H:i:s.u');
echo strftime('%x %X', strtotime($localString));
<?php
$inputDateString = '2018-05-12 23:16:46.123456'; // get from DB or some API
$jsonFormat = 'Y-m-d\TH:i:s.u\Z';
$start = new DateTimeImmutable($inputDateString);
$end = $start->modify('+2 days');
echo json_encode([
'title' => 'Foobar',
'start' => $start->format($jsonFormat),
'end' => $end->format($jsonFormat),