Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Usage of PHP convenience functions

Tuesday, January 25th, 2011 by Servage

Programming should be object oriented. This is a message we get from everywhere nowadays. Back ind the old days, PHP scripts were looong series of includes, functions and global variables. Not anymore. And we should be grateful, because the object oriented way of doing things, really has improved our code, and made it easier to understand and extend. However, there are a few scenarios where I miss the cold old call of a global function that does some kind of minimal functionality. Convenience functions. But is the usage of convenience functions still legitimate today? Is it OK to define a random function somewhere global, and just use it here and there, across your project? I would say: It depends! Let me show you an example where I think, even today, that it’s perfectly reasonable and fine to use global functions.

The MySQL timestamp

Creating timestamps for your MySQL queries with PHP involves the use of the date() and time() functions. If you wish to add the current time to a MySQL query, you need to do something like date(‘Y-m-d’, time()). And how often do you need this? I found to use this type of code line quite often, so I have allowed myself to create a global function called timestamp() which I can call everywhere.

function timestamp($time = false) {
  if(!$time) {
    $time = time();
  return date('Y-m-d H:i:s', $time);
This type of functions is something that I still consider legitimate to use as custom defined global function, because it is used across the entire project, many different places, is is similar to core PHP functions like time() and date().
Usage of PHP convenience functions, 4.3 out of 5 based on 9 ratings
Categories: Guides & Tutorials, Tips & Tricks


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

6 comments (leave a comment)

I agree that functions like this are very useful… though I bundle them into library objects and access them using


or something. This helps organise all your useful (and previously global) functions into libraries.Some of mine are: arrays, dates, debug, files and html.

Great point! That way you can maintain an ordered and elegant collection of functions, that can also easily be integrated in multiple projects!

I would be inclined to disagree with your example although I agree completely that using globally available functions is still useful.

I would have said that such a specific function such as the one you have would go inside the class the manages the database connection. For example if I was using MySQLi or PDO I would extend it and add another method that would handle what you have said for example

class customMySQLi extends mysqli {
public function timestamp($time = null) {
return date(‘Y-m-d H:i:s’, $time);

This is because if you changed your database engine the format of the other engines natural date format may be stored differently (even MySQL has at least 2 date formats).

Also as an alternative you could create static methods on in a class, which would also be globally available throughout your application but helping to keep your global namespace clear (think of your include of functions.php as a class instead)

Great Post. Thanks!

thnx bro

your good

Leave a comment

You must be logged in to post a comment.