我想使用 PHP 的 IntlDateFormater 在我的代码中使用波斯日历。 现在 PHP 有支持波斯日历的内置函数并且工作正常,但我在某些日子有问题。
例如,使用此代码,所有日期都从公历转换为波斯语,直到日期 2018-3-17 转换为“1396-12-26”,之后,日期 2018-3-18 转换为“1397-12- 27"我不知道为什么年份增加了?
我的 php 代码:
class MyDateTime extends \DateTime {
protected $calendar='gregorian';
protected $timezone;
public function __construct() {
$timezone = new \DateTimeZone('Asia/Tehran');
$this->timezone = $timezone;
parent::__construct(null, $timezone);
$this->setCalendar('gregorian');
$this->modify('2018-2-19');
}
public function setCalendar($calendar) {
$this->calendar = strtolower($calendar);
return $this;
}
public function format($pattern){
$formater = new \IntlDateFormatter('en_US' . '@calendar=' . $this->calendar,
\IntlDateFormatter::FULL, \IntlDateFormatter::FULL, $this->timezone,
$this->calendar == 'gregorian' ? \IntlDateFormatter::GREGORIAN : \IntlDateFormatter::TRADITIONAL, $pattern);
return $formater->format(parent::format('U'));
}
}
$d = new MyDateTime();
$d->setCalendar('gregorian');
echo $d->format('Y-M-d');
$d->setCalendar('persian');
echo " Persian: ".$d->format('Y-M-d')."<br >\n";
echo 'Next mpnth:'." <br>\n";
$d->modify('+26 day');
$d->setCalendar('gregorian');
echo $d->format('Y-M-d');
$d->setCalendar('persian');
echo " Persian: ".$d->format('Y-M-d')."<br >\n";
echo 'Next day:'." <br>\n";
$d->modify('+1 day');
$d->setCalendar('gregorian');
echo $d->format('Y-M-d');
$d->setCalendar('persian');
echo " Persian: ".$d->format('Y-M-d')."<br >\n";
输出:
2018-2-19 Persian: 1396-11-30
Next mpnth:
2018-3-17 Persian: 1396-12-26
Next day:
2018-3-18 Persian: 1397-12-27
最佳答案
不知道为什么您的代码不起作用。旧的 PHP 版本?这适用于 PHP 7.2.21:
function dateGregorianToPersian($strDate)
{
$tz = new DateTimeZone('Asia/Tehran');
$fmt = datefmt_create(
"ir_IR@calendar=persian",
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
$tz,
IntlDateFormatter::TRADITIONAL,
"yyyy-MM-dd"
);
return datefmt_format( $fmt ,date_create($strDate,$tz));
}
//test
$data = ['2018-2-19','2018-3-17','2018-3-18'];
foreach($data as $strDate){
echo $strDate.' Persian: '.dateGregorianToPersian($strDate).'<br>';
}
输出
2018-2-19 Persian: 1396-11-30
2018-3-17 Persian: 1396-12-26
2018-3-18 Persian: 1396-12-27
关于php - 在某些日子里从公历转换为波斯历时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48865051/
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
我正在使用Rails构建一个简单的聊天应用程序。当用户输入url时,我希望将其输出为html链接(即“url”)。我想知道在Ruby中是否有任何库或众所周知的方法可以做到这一点。如果没有,我有一些不错的正则表达式示例代码可以使用... 最佳答案 查看auto_linkRails提供的辅助方法。这会将所有URL和电子邮件地址变成可点击的链接(htmlanchor标记)。这是文档中的代码示例。auto_link("Gotohttp://www.rubyonrails.organdsayhellotodavid@loudthinking.
我收到格式为的回复#我需要将其转换为哈希值(针对活跃商家)。目前我正在遍历变量并执行此操作:response.instance_variables.eachdo|r|my_hash.merge!(r.to_s.delete("@").intern=>response.instance_eval(r.to_s.delete("@")))end这有效,它将生成{:first="charlie",:last=>"kelly"},但它似乎有点hacky和不稳定。有更好的方法吗?编辑:我刚刚意识到我可以使用instance_variable_get作为该等式的第二部分,但这仍然是主要问题。
运行bundleinstall后出现此错误:Gem::Package::FormatError:nometadatafoundin/Users/jeanosorio/.rvm/gems/ruby-1.9.3-p286/cache/libv8-3.11.8.13-x86_64-darwin-12.gemAnerroroccurredwhileinstallinglibv8(3.11.8.13),andBundlercannotcontinue.Makesurethat`geminstalllibv8-v'3.11.8.13'`succeedsbeforebundling.我试试gemin
2022/8/4更新支持加入水印水印必须包含透明图像,并且水印图像大小要等于原图像的大小pythonconvert_image_to_video.py-f30-mwatermark.pngim_dirout.mkv2022/6/21更新让命令行参数更加易用新的命令行使用方法pythonconvert_image_to_video.py-f30im_dirout.mkvFFMPEG命令行转换一组JPG图像到视频时,是将这组图像视为MJPG流。我需要转换一组PNG图像到视频,FFMPEG就不认了。pyav内置了ffmpeg库,不需要系统带有ffmpeg工具因此我使用ffmpeg的python包装p