Wanna run a full marathon?

If you are a runner like me, and set a goal on running your first marathon you might be of need for a training plan.

Sure you may think, hey I know myself, feet are feet, I already ran half marathons, what’s the difference… a full marathon cannot be that more difficult. Hmm, believe me it is. It simply does not need some more training, it needs a great deal more and most important… it needs you to know you are doing the right thing even when you body talks back.

So… first you have to learn to distinguish ‘good pain’ (discomfort from leaving your comfort zone) from ‘bad pain’ (due to injury), then you need to ignore ‘good pain’, and use a plan on how to increase your training volume without overdoing it.

Here some training plans I found very useful.

PS: 3 weeks before my first full marathon…

Backup your files in a cronjob by a php script

The other day I considered setting up a cronjob for my site regularly deleting some temp files.

After Hours Programming warns against deleting files in php because there is no special php undo for what you delete.

So I reconsidered deleting the files and started thinking about how to backup them instead.

A quick search in internet helped me find a php function for recursive_copy_file provided by Guilherme Serrano in GitHubGist. I modified my script to copy by another file name while copying it into a backup folder and delete the original files afterwards. This way I could in case I would like to, recall the content of the files without them actually still be accessible in their original location. I don’t care about their actual names or creation dates.

For my script to know which folder it should start checking, I’ve put it in the folder it should backup and used getcwd()

However, if my script was to delete all files in the directory and leave my script still on position, it had to check the extension of the pathinfo() and make sure it won’t do nothing for “php”, or only copy files of another specific extension.

But still, even if it renames after copying, and deletes the original files, this isn’t saving much webspace. To solve this, I used rename() instead of copy(), compressed the copied files thus in a gz and unlink() afterwards the renamed copy.

Finally, back in the control center of my webspace I only needed to select this script in the Cronjob manager as a new cronjob and define the intervals for this action to be performed automatically.

Enjoy…

strtotime – Converting a timestamp into date

You can use the strtotime () php function to convert a string to time. When converting easy stuff like ’05/29/2019′, or ‘2019-05-29’, things are easy and predictable:

$string ="05/29/2019";
print $string ." => ". date('Y-m-d',strtotime($string));
$string ="2029-05-29";
print $string ." => ". date('Y-m-d',strtotime($string));
$string ="29.05.2019";
print $string ." => ". date('Y-m-d',strtotime($string))";

All of above will output: 2019-05-29

Which also works fine when using strings with times:

$string ="05/29/2019 16:05:01";
print $string ." => ". date('Y-m-d H:i:s',strtotime($string));
$string ="20190529T160501";
print $string ." => ". date('Y-m-d H:i:s',strtotime($string));

All of above will output: 2019-05-29 16:05:01 

When converting times you’ll need to be aware that timestamps such as ‘20190529T160501Z’ define time in UTC and their conversion will happen according to the default_timezone of your php. So…

$string ="20190529T160000Z";
print $string ." => ". date('Y-m-d H:i:s T',strtotime($string)); 
print "!!! Time conversion based on the current php time zone: ".date_default_timezone_get()." !!!";

This will output time according to the current timezone. My output would be:

2019-05-29 17:00:00 BST !!! Time zone conversion based on the current time zone php setting: Europe/London !!!

If you need to convert time in a specific timezone you need to set the default_timezone first…

$string ="20190529T160000Z";
date_default_timezone_set('America/Chicago');
print $string ." => ". date('Y-m-d H:i:s T',strtotime($string));
print "!!! Time conversion based on the current php time zone: ".date_default_timezone_get()." !!!";

Which outputs now:

2019-05-29 11:00:00 CDT !!! Time zone conversion based on the current time zone php setting: America/Chicago !!!

To convert any time to a specific time zone, need to use the “UTC” parameter:

$string ="05/29/2019 16:05:01";
date_default_timezone_set('America/Chicago');
print $string ." => ". date('Y-m-d H:i:s T',strtotime($string." UTC"))

Remember: Once you’ve used strtotime for a UTC timestamp you don’t need to do yet another conversion. Just make sure you have the correct timezone set before calling strtotime