使用 Carbon 在 php 中使用 date-fns 格式化样式
我有一个应用程序,我的用户可以在其中输入日期对象的格式字符串。我使用 date-fns 来格式化日期(使用 这种格式)在前端完美运行:
import { format } from "date-fns";
import { es } from "date-fns/locale";
const formatStr = "yyyy-MM-dd" // user defined
format(date, formatStr, { locale: es }); // returns "2022-03-23"
问题是现在我必须在 php.ini 中使用相同的格式。我以为 Carbon 可以完成这项工作,但它实际上使用 php 日期格式化样式。
$formatStr = "yyyy-MM-dd"; // same value as before
Carbon::parse($date)->format($formatStr) // returns "22222222-MarMar-2323"
有没有办法将 date-fns 格式样式“翻译”为 Carbon,或者我应该使用另一个库?
I have an app where my users can enter a format string for a date object. I use date-fns to format the dates (with this format) and it works perfectly on the front end:
import { format } from "date-fns";
import { es } from "date-fns/locale";
const formatStr = "yyyy-MM-dd" // user defined
format(date, formatStr, { locale: es }); // returns "2022-03-23"
The problem is that now I have to use the same format in php. I thought Carbon would do the job, but it actually uses php date formatting style.
$formatStr = "yyyy-MM-dd"; // same value as before
Carbon::parse($date)->format($formatStr) // returns "22222222-MarMar-2323"
Is there any way to "translate" date-fns formatting style to Carbon, or should I use another library?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Carbon 有一个接受 momentjs 风格字符串的
isoFormat()
。从他们的文档:所以您应该能够使用
Carbon has an
isoFormat()
that accepts a momentjs-style string. From their documentation:So you should be able to use