Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #15791

Re: Looking for NULL in arrays

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.php
Subject Re: Looking for NULL in arrays
Date 2015-10-26 22:25 +0100
Organization PointedEars Software (PES)
Message-ID <2317665.9IWv2tE1in@PointedEars.de> (permalink)
References <d94ctvFn9f4U1@mid.individual.net> <n0je5n$jqn$1@dont-email.me> <d96arrF7egqU1@mid.individual.net>

Show all headers | View raw


Derek Turner wrote:

> The task is to construct a diary of meetings for the next three months
> from that information,  not a trivial task. Playing with strtotime I've
> discovered that it's terribly flaky an unpredictable, mostly because it
> interprets "fifth Sunday of February" (for example) as '5th Sunday after
> Feb 1st' rather than throwing an error as one might naively expect.

I predicted that to you here a month ago.
 
> So I've found by experimenting that the most reliable way to get the
> results I want is to supply a timestamp to strtotime to work from for
> each month's results.

I still think it would be better to store “fifth Sunday of February” as the 
tuple (2[=February], 0[=Sunday], 5[=fifth occurrence]) or something like 
that.

> It needs to be the last day of the previous month to give reliable
> results.

I do not think so.
 
> The plan is to populate a temporary table of meetings which can then be
> SELECTed from and ORDERed BY.

OK.
 
> I will need to iterate through the 4 offsets (last day of previous month)
> and through each of the string ordinals (3 or 5, may contains NULL values)

At this point you should realize that your approach is too complicated, 
therefore probably wrong.  And in this case it is definitely wrong.
 
> […]
> $first = strtotime($first) - 86400;

Not every day in every time zone has 86'400 seconds.

MySQL and PHP have *date* functions, and PHP has a *Date*Time class for a 
reason.

For example, you can *calculate* the date of the last Sunday in April 2015 
this way:

php -r '

$mayTheFirst = new DateTime("2015-05-01");
$lastDayInApril = $mayTheFirst->modify("-1 day");
$lastSundayInApril = $lastDayInApril->modify(
  sprintf("-%d days", $lastDayInApril->format("w")));

var_dump($lastSundayInApril);

'
class DateTime#1 (3) {
  public $date =>
  string(26) "2015-04-26 00:00:00.000000"
  public $timezone_type =>
  int(3)
  public $timezone =>
  string(13) "Europe/Berlin"
} 

(This simple, unoptimized, undertested, off-the-top-of-my-hat solution works 
for Sundays only: If the last day in the month is a Sunday, you do not 
subtract from the date [you subtract 0, the weekday number for Sunday], 
otherwise you subtract from the date the number of weekdays to the previous 
Sunday.  For example, if the last day of the month is a Monday, it has 
weekday number 1, which is exactly the number of days to the previous 
Sunday.)

-- 
PointedEars
Zend Certified PHP Engineer
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Looking for NULL in arrays Derek Turner <frderek@suremail.je> - 2015-10-25 16:05 +0000
  Re: Looking for NULL in arrays Derek Turner <frderek@suremail.je> - 2015-10-25 16:11 +0000
    Re: Looking for NULL in arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-25 19:07 +0100
  Re: Looking for NULL in arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-25 19:06 +0100
    Re: Looking for NULL in arrays Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2015-10-25 21:01 +0100
      Re: Looking for NULL in arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-25 21:58 +0100
  Re: Looking for NULL in arrays Jerry Stuckle <jstucklex@attglobal.net> - 2015-10-25 16:33 -0400
    Re: Looking for NULL in arrays Derek Turner <frderek@suremail.je> - 2015-10-26 09:42 +0000
      Re: Looking for NULL in arrays Jerry Stuckle <jstucklex@attglobal.net> - 2015-10-26 09:45 -0400
        Re: Looking for NULL in arrays Derek Turner <frderek@suremail.je> - 2015-10-26 13:57 +0000
          Re: Looking for NULL in arrays Erwin Moller <erwinmollerusenet@xs4all.nl> - 2015-10-26 17:41 +0100
          Re: Looking for NULL in arrays Jerry Stuckle <jstucklex@attglobal.net> - 2015-10-26 12:49 -0400
      Re: Looking for NULL in arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-26 22:25 +0100
  Re: Looking for NULL in arrays Denis McMahon <denismfmcmahon@gmail.com> - 2015-10-27 03:16 +0000
    Re: Looking for NULL in arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-27 22:23 +0100

csiph-web