使用 Carbon 在 php 中使用 date-fns 格式化样式

发布于 2025-01-16 13:57:29 字数 820 浏览 0 评论 0原文

我有一个应用程序,我的用户可以在其中输入日期对象的格式字符串。我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱你不解释 2025-01-23 13:57:29

Carbon 有一个接受 momentjs 风格字符串的 isoFormat() 。从他们的文档

->isoFormat(string $format):字符串使用 ISO 格式而不是 PHP 特定的格式,并使用内部翻译而不是需要在部署应用程序的每台计算机上安装的语言包。 isoFormat 方法与 momentjs 格式方法兼容,这意味着您可以使用与在前端或 Node.js 中使用的相同格式字符串。以下是一些示例:

$date = Carbon::parse('2018-06-15 17:34:15.984512', 'UTC');
echo $date->isoFormat('MMMM Do YYYY, h:mm:ss a'); // June 15th 2018, 5:34:15 pm
echo "\n";
echo $date->isoFormat('dddd');           // Friday
echo "\n";
echo $date->isoFormat('MMM Do YY');      // Jun 15th 18
echo "\n";
echo $date->isoFormat('YYYY [escaped] YYYY'); // 2018 escaped 2018

所以您应该能够使用

Carbon::parse($date)->isoFormat($formatStr)

Carbon has an isoFormat() that accepts a momentjs-style string. From their documentation:

->isoFormat(string $format): string use ISO format rather than PHP-specific format and use inner translations rather than language packages you need to install on every machine where you deploy your application. isoFormat method is compatible with momentjs format method, it means you can use same format strings as you may have used in moment from front-end or node.js. Here are some examples:

$date = Carbon::parse('2018-06-15 17:34:15.984512', 'UTC');
echo $date->isoFormat('MMMM Do YYYY, h:mm:ss a'); // June 15th 2018, 5:34:15 pm
echo "\n";
echo $date->isoFormat('dddd');           // Friday
echo "\n";
echo $date->isoFormat('MMM Do YY');      // Jun 15th 18
echo "\n";
echo $date->isoFormat('YYYY [escaped] YYYY'); // 2018 escaped 2018

So you should be able to use

Carbon::parse($date)->isoFormat($formatStr)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文