“date(): It is not safe to rely on the system’s timezone settings…”

You probably need to put the timezone in a configuration line in your php.ini file. You should have a block like this in your php.ini file: [Date] ; Defines the default timezone used by the date functions ; http://php.net/date.timezone date.timezone = America/New_York If not, add it (replacing the timezone by yours). After configuring, make sure … Read more

PHP shell_exec() vs exec()

shell_exec returns all of the output stream as a string. exec returns the last line of the output by default, but can provide all output as an array specifed as the second parameter. See http://php.net/manual/en/function.shell-exec.php http://php.net/manual/en/function.exec.php

Default value in Doctrine

<?php /** * @Entity */ class myEntity { /** * @var string * * @ORM\Column(name=”myColumn”, type=”integer”, options={“default” : 0}) */ private $myColumn; … } Note that this uses SQL DEFAULT, which is not supported for some fields like BLOB and TEXT.

Invalid argument supplied for foreach()

Personally I find this to be the most clean – not sure if it’s the most efficient, mind! if (is_array($values) || is_object($values)) { foreach ($values as $value) { … } } The reason for my preference is it doesn’t allocate an empty array when you’ve got nothing to begin with anyway.

Using str_replace so that it only acts on the first match?

There’s no version of it, but the solution isn’t hacky at all. $pos = strpos($haystack, $needle); if ($pos !== false) { $newstring = substr_replace($haystack, $replace, $pos, strlen($needle)); } Pretty easy, and saves the performance penalty of regular expressions. Bonus: If you want to replace last occurrence, just use strrpos in place of strpos.

Traits vs. interfaces

Public Service Announcement: I want to state for the record that I believe traits are almost always a code smell and should be avoided in favor of composition. It’s my opinion that single inheritance is frequently abused to the point of being an anti-pattern and multiple inheritance only compounds this problem. You’ll be much better … Read more

Call a REST API in PHP

You can access any REST API with PHPs cURL Extension. However, the API Documentation (Methods, Parameters etc.) must be provided by your Client! Example: // Method: POST, PUT, GET etc // Data: array(“param” => “value”) ==> index.php?param=value function CallAPI($method, $url, $data = false) { $curl = curl_init(); switch ($method) { case “POST”: curl_setopt($curl, CURLOPT_POST, 1); … Read more

PHP date() format when inserting into datetime in MySQL

The problem is that you’re using ‘M’ and ‘D’, which are a textual representations, MySQL is expecting a numeric representation of the format 2010-02-06 19:30:13 Try: date(‘Y-m-d H:i:s’) which uses the numeric equivalents. edit: switched G to H, though it may not have impact, you probably want to use 24-hour format with leading 0s.