PeanutsMonkey
PeanutsMonkey

Reputation: 7095

PHP strtotime function quandry

I am new to the world of coding as well as PHP and befuddled by what the '-01' does in the code below.

strtotime($someYear . '-' . $someMonth . '-01');

Upvotes: 0

Views: 73

Answers (2)

fredrik
fredrik

Reputation: 13540

Sets the day to the first in month, i.e if $someYear is '1999' and $someMonth is '01' this would yield

strtotime("1999-01-01");

Upvotes: 3

John Carter
John Carter

Reputation: 55271

It's selecting the first day of the month.

. is the string concatenation operator in PHP, and PHP will convert numbers to strings automatically.

So let's say you had:

$someYear = 2011; 
$someMonth = 5; 

And you want the timestamp of the start of that month, you could it do it with that:

$someDate = $someYear . '-' . $someMonth . '-01';  // equivalent to "2011-5-01"
strtotime($someDate);

This is a bit ugly, but it works.

Upvotes: 5

Related Questions