views:

68

answers:

4

My background is C, Java, C#, and VB.NET, but I have to work on some basic PHP stuff. I've gotten to this line of code:

if($flag) $event_end--;

What exactly is being decremented? The raw ticks? $flag (I think) represents if the event is an all-day event, where the start would be 7/1/2010 and end would be 7/2/2010. Does the -- essentially make $event_end [7/1/2010 11:59:59.99999 PM]?

-- edit: ok, cardinal rule of technical questions: explain what you're talking about. guilty. $event_end is orginally being set using the mktime() function. does that help?

A: 

It depends entirely how $event_end has been set: PHP has no native concept of a 'datetime'. It might be seconds, milliseconds, or even days depending on where it came from.

Colin Fine
PHP *does* have a DateTime class: http://us2.php.net/manual/en/class.datetime.php
Adam Backstrom
DateTime is an extension, which you can use or not: it's not a part of the language. And the OP gave no indication that they were using DateTime.
Colin Fine
From manual: `Installation: There is no installation needed to use these functions; they are part of the PHP core.` (since 5.2)And the OP used the word 'datetime' in the question title
Mchl
A: 

You mean a DateTime object? As far as I can tell nothing happens at all.

 >> $a = new DateTime()
 DateTime::__set_state(array(
    'date' => '2010-07-30 12:32:22',
    'timezone_type' => 3,
    'timezone' => 'UTC',
 ))
 >> $a--;
 DateTime::__set_state(array(
    'date' => '2010-07-30 12:32:22',
    'timezone_type' => 3,
    'timezone' => 'UTC',
 ))

If you mean date/time stored as string, refer to how strings are cast to numbers section in manual.

Mchl
+1  A: 

Has a function such as strtotime() been called on the datetime first? This function will return the Unix Timestamp corresponding to the date and time. If the time is 0 (i.e. midnight), subtracting 1 from it (i.e. taking away 1 second), the result will be a time of 23:59:59.

$event_end = '2010-07-30 00:00:00';
$event_end = strtotime($event_end);
$event_end--;
$event_end = date("Y-m-d H:i:s",$event_end);
echo $event_end;  //This will result in '2010-07-29 23:59:59'

PHP sees datetime values (as queried from MySQL for example) as simple strings unless they are DateTime objects in which case I don't think subtracting from it would do anything. The only way that operator would affect the value is if it was a timestamp integer.

EDIT: Ah, I see it uses mktime(). This also returns a Unix Timestamp and therefore can be operated on mathematically. So, to answer your question, indeed the $event_end--; is subtracting one second.

Updated Code:

$event_end = mktime(0, 0, 0, 7, 30, 2010);
$event_end--;
$event_end = date("Y-m-d H:i:s",$event_end);
echo $event_end;  //This will result in '2010-07-29 23:59:59'
Brendan Bullen
A: 

If $event_end is set by mktime() then it is a long integer containing the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

$event_end-- will reduce the number by one (one second).

Scott Saunders