jeudi 26 mai 2016

How to test relative dates in PHP

I've got a code snippet like this (PHP 5.3):

  <?php
  $last_sunday = new DateTime();
  $last_sunday->modify("Sunday last week");
  $last_sunday->setTime(0, 0, 0);

  $last_saturday = new DateTime();
  $last_saturday->modify("Saturday last week");
  $last_saturday->setTime(0, 0, 0);

  $next_sunday = new DateTime();
  $next_sunday->modify("Sunday this week");
  $next_sunday->setTime(0, 0, 0);

  $this_saturday = new DateTime();
  $this_saturday->modify("Saturday this week");
  $this_saturday->setTime(0, 0, 0);

What I'd like to figure out is how to test this (a la PHPUnit or some other framework - or even just via manual testing) for what is returned based on the current day of the week. I've noticed interesting behaviors especially around Sundays. I've tried to reset the date on my VM (using a CentOS 6 image on VirtualBox) but that isn't working.

If today is May 26 I can manually test and see that it works. The following print out as values if I print ->format('m-d h:i a'):

05-22 12:00 am // $last_sunday
05-21 12:00 am // $last_saturday
05-29 12:00 am // $next_sunday
05-28 12:00 am // $this_saturday
05-26 11:09 am // this is today

If it isn't clear, I'm simply trying to determine a reliable means for setting the weekend dates before and after (in America where calendar week starts on Sunday). Thanks for any suggestions.

Aucun commentaire:

Enregistrer un commentaire