簡體   English   中英

Carbon將天數轉換為人類可讀的格式

[英]Carbon convert no of days to human readable format

我需要將30天轉換為1個月。如果要用幾個月和幾天,那么像1年2個月2天,我在下面嘗試過,但是會返回錯誤的結果

 echo CarbonInterval::days(30)->cascade()->forHumans();

誰能幫助我實現這一目標?

我已經嘗試過以下解決方案,但相差僅2天

$convert = '30'; // days you want to convert

    $years = ($convert / 365) ; // days / 365 days
    $years = floor($years); // Remove all decimals

    $month = ($convert % 365) / 30.5; // I choose 30.5 for Month (30,31) ;)
    $month = floor($month); // Remove all decimals

    $days = ($convert % 365) % 30.5; // the rest of days

    // Echo all information set
    echo 'DAYS RECEIVE : '.$convert.' days<br>';
    echo $years.' years - '.$month.' month - '.$days.' days';

使用碳有什么好的解決方案

是否必須是CarbonInterval?

那么Carbon::now()->subDays(1827)->diffForHumans()呢?

不能按預期工作的原因(來自https://carbon.nesbot.com/docs/#api-interval ):

默認因素是:

  • 1分鍾= 60秒
  • 1小時= 60分鍾
  • 1天= 24小時
  • 1周= 7天
  • 1個月= 4周
  • 1年= 12個月

CarbonIntervals不帶有上下文,因此它們不能更加精確(沒有DST,沒有leap年,沒有實際月份長度或年份長度考慮)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM