dougu2006 2012-08-15 15:20
浏览 77
已采纳

如果给定的年份和季度在另一个之前或之后,可靠的方法来计算?

E.g. given:

$quarter1 = 4;
$year1 = 2011;

$quarter2 = 3;
$year2 = 2012;

I need to know that the second set are in the future compared to the first set.

I did think this could be done by literally appending the quarter onto the year. e.g.

$qy1 = 20114;
$qy2 = 20123;
// $qy1 < $qy2

But I'm sure this is not a reliable way to compare the two. Is there a better, more reliable way?

Many thanks

  • 写回答

3条回答 默认 最新

  • dongxia19772008 2012-08-15 15:28
    关注

    Nothing wrong with your method, other than having to do a few string concatenations and typecasting back to int. It could fail, however, if you fail to force the values back to integers, and you're comparing years with different string lengths, e.g.:

    $date1 = '9994';  // year 999, q4
    $date2 = '10011'; // year 1001, q1
    

    String 9994 will compare as larger than string 10011.

    Purely as an alternative, this would work as well, keeping things purely numeric:

    $date1 = $year1 + (($quarter1 - 1) / 4);
    $date2 = $year2 + (($quarter2 - 1) / 4);
    if ($date1 < $date2) { ... }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备