Preg_Replace all strings between two tags
Last Updated on Friday, 08 March 2013
For those of you who use Preg_Replace. Preg_replace is a function that uses regular expressions to search and replace a string.
Why?
Because my understanding with regular expressions is shady and varies from language to language, I've written this article as a quick reference point.
How?
Php convert filesizes to bytes kb mb gb
Last Updated on Thursday, 03 January 2013
Just a quick note on how to format a given filesize and to reduce the display output to a small string, eg:
- 196 bytes : displays as => "196 bytes"
- 12945 bytes : displays as => "12 Kb"
- 1478515 bytes : displays as => "1 Mb"
- 8798745455 bytes : displays as => "8 Gb"
Convert seconds to total time in PHP
Last Updated on Tuesday, 31 January 2012
This is intended for activity/session durations and although I usually get MySQL to do the date/time calculations, there are times when we have to do with PHP. This is the shortest way I know to properly convert seconds into total hours, minutes and seconds (taking into account regional settings and without using a date function).
How?
- $total_time =intval(intval($total_seconds)/ 3600).":";
- $total_time.=str_pad(intval(($total_seconds/60)%60),2,"0",STR_PAD_LEFT).":";
- $total_time.=str_pad(intval($total_seconds%60),2,"0",STR_PAD_LEFT);
- // yields
- // 82800 = 23:00:00
- // 108000 = 30:00:00
- $total_time=intval(intval($total_seconds)/ 3600).":".str_pad(intval(($total_seconds/60)%60),2,"0",STR_PAD_LEFT).":".str_pad(intval($total_seconds%60),2,"0",STR_PAD_LEFT);
First and Last Entry on a Page using Modulus Remainder
Last Updated on Tuesday, 27 September 2011
This article is a quick note (so I never spend as long again) in PHP on how to determine when looping through a loop, which entry was first and which was last. This is incredibly useful for pagination.
Difference between two dates - the midnight hour
Last Updated on Monday, 26 September 2011
Now bear in mind the below is in European date format
Consider the following:
- Date Customer Time From Time To Hours
- ---------- --------------- --------------- ---------------- --------------
- 04/03/2011 Tweedle Dee 10:00 12:00 2.00
- 08/03/2011 Tweedle Dum 23:30 00:30 -23.00
- $thisDateSQL=date("Y-m-d", strtotime($sub_row['DateSession']));
- $this_time_from1 = date("H:i", strtotime($sub_row['TimeFromSession']));
- $this_time_to1 = date("H:i", strtotime($sub_row['TimeToSession']));
- $this_time_from_sql=$thisDateSQL." ".$this_time_from1.":00";
- $this_time_to_sql=$thisDateSQL." ".$this_time_to1.":00";
- $sum_hours = number_format(((strtotime($this_time_from_sql)-strtotime($this_time_from_sql))/60)/60, 2);
- // Using the examples above this is doing the following:
- 2011-03-04 12:00:00 - 2011-03-04 10:00:00 = 2.00
- 2011-03-08 00:30:00 - 2011-03-08 23:30:00 = -23.00

