Skip to content

Instantly share code, notes, and snippets.

<?php
$dateString = rtrim($_POST['date'], 'Z');
$date = new DateTimeImmutable("$dateString Asia/Tokyo");
echo $date->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d H:i:s.u');
var date = new Date(dateString + 'T' + timeString + 'Z');
<?php
$date = new DateTimeImmutable($_POST['date']);
echo $date->format('Y-m-d H:i:s.u');
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,
]);