CakeTime 31/12/2012 to 2012-12-31? - cakephp

I am trying to format an date received as 31/12/2012 to 2012-12-31.
I have used ::format and ::toServer, but I get 2012-31-12 instead of 2012-12-31.
How can I do it ?
Thank you!

Try this:
//via Time helper
echo $this->Time->format('Y-m-d', "31/12/2012");
//via CakeTime utility
echo CakeTime::format('Y-m-d', "31/12/2012");
From CakePHP 1.3 manual:
(...) format is a wrapper for the PHP date function.

Related

Format Date momentjs

I would like to convert a date with momentjs, the date is on this format:
2021-10-10T00:00:00+02:00
In react js I do:
moment('2021-10-10T00:00:00+02:00').format('dd/mm/yyyy');
And that return me "invalid date"
Have you got any idea about this error ?
Just have the format specified inside the moment constructor. Check the snippet below.
For different formatting options, check their docs https://momentjs.com/docs/#/displaying/format/
const date = "2021-10-10T00:00:00+02:00";
const formatted = moment(date, "YYYY-MM-DD hh:mm:ss+ZZ").format("DD/MM/YYYY");
console.log(formatted);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>
Check this codesandbox which seems to be working with your input. Check if your import of moment has an issue.
CodeSandbox
Also note that if you want to format month it should be capital M - moment("2021-10-10T00:00:00+02:00").format("dd/MM/yyyy")
You code is work for me. Please check your date input is same as you type.
var time = moment('2021-10-10T00:00:00+02:00').format('DD/MM/YYYY');
$("body").text("The time is: "+time+".");
result:
The time is: 10/10/2021.
If you know the format of an input string, you can use that to parse a moment.
moment("2021-10-10T00:00:00+02:00", "YYYY-MM-DDTHH:mm:ss+-HH:mm");
If a time part is included, an offset from UTC can also be included as +-HH:mm, +-HHmm, +-HH or Z.
2013-02-08 09+07:00 # +-HH:mm
2013-02-08 09-0100 # +-HHmm
2013-02-08 09Z # Z
2013-02-08 09:30:26.123+07:00 # +-HH:mm
2013-02-08 09:30:26.123+07 # +-HH
Moment Docs - Parse - String

default locale change, version 5.0.1 -> 5.1

(sorry my english) Hi.
With 5.0.1, the default short date format for locale "es" (in config.json) return this date format "10/01/2017"
With 5.1, the default short date format for locale "es" (in config.json) return this date format "10 ene. 2017".
I think is a coherent change. But I need keep working with the old format. So, where I must touch to get the old format in a entire new 5.1 qooxdoo project?
I mean, where this locale format is defined? I was trying found where but I can't.
Or any other solution.
thanks
This snippet works for us
this._localeManager = qx.locale.Manager.getInstance();
this._localeManager.addLocale("el", {
"cldr_date_format_short": "dd/MM/yyyy" // Override short date format for Greek
});

Issue with converting a date using Carbon

$value="Mar 10 2016 09:12:03:000AM";
return Carbon::createFromFormat('Y-m-d H:i:s.u0', $value);
Please will someone please tell me how to use Carbon to
convert this date to that format.
I'm getting:
InvalidArgumentException in Carbon.php line 425:
Unexpected data found.
Unexpected data found.
The separation symbol could not be found
Unexpected data found.
The format separator does not match
Trailing data
use carbon like
$value="Mar 10 2016 09:12:03:000AM";
return \Carbon\Carbon::parse($value)->format('Y-m-d H:i:s.u0');
For detail about how to carbon module used please visit on link Carbon Docs
In controller declare use Carbon\Carbon;
$value="Mar 10 2016 09:12:03:000AM";
return Carbon::parse($value);
$value="Mar 10 2016 09:12:03:000AM";
Carbon::parse($value)->format('Y-m-d H:i:s.u0');
Use parse method to instantiate the $value string as carbon object and then you can use any of the helper functions available in Carbon Library.
Refer to the given link :: http://carbon.nesbot.com/docs/#api-formatting.

cakephp 3 displaying date without time

CakePHP 3: I have a database field which is a DATE (not DATETIME nor TIMESTAMP)
When I display
echo $contact->date;
It will show something like 2014. 01. 06. 0:00. How to hide hours and minutes?
I tried
print $this->Time->format($contact->date, 'Y-m-d');
but I got 2014-0-6
How to get year-month-day?
rrd
Have you tried this?
echo $contact->date->format('Y-m-d');
add in App\Controller:
use Cake\I18n\Time;
Time::$defaultLocale = 'es-ES';
Time::setToStringFormat('YYYY-MM-dd');
You can directly print the date object in any custom date String format by using the inbuilt i18nFormat function.
$frozenDateObj->i18nFormat('dd-MMM-yyyy');
Use Datetime Format Syntax reference for more customization
If need all over the project with specific format you can use
boostrap.php
Cake\I18n\FrozenDate::setToStringFormat('yyyy-MM-dd');
echo date_format($contact->date, 'Y-m-d'); //php format
try
date('Y-m-d',strtotime($contact->date));

Translate Month-Day combinations in CakePHP

I can translate an individual month or day just fine using my .po files:
echo __('December'); //becomes diciembre
echo __('Thursday'); //becomes jueves
//...etc
But, when I use a date formate like this:
echo __(date("j F, Y")); //becomes 20 December 2012
It doesn't translate - I assume because I have translations for each month and day in individual lines.
Normally I would just do something like this:
__(date('j')) . ' ' . __(date('F')) . ' ' . __(date('Y'));
But, in the CMS, the admin is allowed to change the date to any format they want. So, it could be "j F, Y", or "Y-m-d", or... anything else.
I thought maybe I could make a helper or something, that broke apart a date into pieces, and returns each part in a __(), but - this seems overkill. Is there an easy way to do this?
I am setting my locale in the AppController:
setlocale(LC_ALL, $currentLanguage['locale']);
Configure::write('Config.language', $currentLanguage['code2']);
Turns out CakePHP has a TimeHelper i18nFormat function:
$time = time();
$timestring = $this->Time->format('Y-m-d H:i:s', $time);
$this->Time->i18nFormat($timestring, "%A %e %B %Y");
Create a file "LC_TIME" (no extension) and put it in your /Locale/ara/ folder (or replace 'ara' with whatever 3-char language code you want)
Copy the contents of CakePHP's time_test LC_TIME file and put it into yours (then save of course).
Then change it's contents to whatever language you want (I believe that example is in Spanish).
That's it!
Notes:
More details about the LC_TIME file here: http://pic.dhe.ibm.com/infocenter/aix/v6r1/index.jsp?topic=%2Fcom.ibm.aix.files%2Fdoc%2Faixfiles%2FLC_TIME.htm
The CakeTime class (and thus the TimeHelper) uses the 'cake' domain for day and month names translation. So put those translations in cake.po file instead of default.po

Resources