General PHP code snippets
A collection of bits and pieces in PHP
Where am I?
Read all files in directory into array
Set timezone to local
Using mobile or tablet?
Where am I?
exec('pwd',$return); print_r($return);
Read all files in directory into array
I couldn’t get scandir working – maybe a problem with permissions? But this one does work …
$dir = 'subdirectory/subsubdir/etc'; $files = []; if ($handle = opendir($dir)) { while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != "..") { array_push($files, $entry); } } closedir($handle); }
Set timezone to local
To set the timezone to Europe/London you need to do this:
<? date_default_timezone_set('Europe/London'); ?>
Using mobile or tablet?
This function checks to see if the user is using a mobile phone or tablet without the need for media queries in CSS. Was working in 2022.
function isMobileDevice() { return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]); } }
Thursday 17 March 2022, 590 views
Previous post: Date and time utilities in PHP Programming index
- General PHP code snippets
- Date and time utilities in PHP
- How to get rid of “episodes and shows” from Spotify:
- Example of character counter using Bootstrap and jQuery
- How to create a simple Twitter bot
- Finding the first and last day of a month in PHP
- Javascript countdown timer
- Creating a WordPress theme from scratch
Leave a Reply