| ||||||
|
How to validate a date without using regular expressions in PHP
Although regular expressions always look impressive, and are a good way of confusing non-programmers (and some programmers) while coming across as a rocket scientists, there are some instances when regular expressions are indeed not required. And validating a date is one of them. The main concern of course when validating a date entered by a user is that the day specified is not beyond the amount of days to which the month is limited. The following function, checks that no nonsense dates have been entered (the 30th of February for instance) among other things: <?php $day = 17; $month = 8; $year = 2009; echo checkdate($month, $day, $year); ?> The function will either return 1 (true) if valid, or 0 (false) if not valid. The year has to be between 0 and 32767. Of course for February leap years will be taken into account.
Posted by Garren Harland on August 17, 2009, 4:17 pm.
Let's say you want to plan your Easter weekend for 2016. Naturally enough to do this you will require the exact date that Easter falls on. You can now do two things. Go look up the date in a calendar, or simply make use of a predefined PHP function capable of returning every Easter that has or will occur during the Unix era. Click here to read on.
Posted by Garren Harland on August 5, 2009, 3:20 pm.
To calculate the remainder of a subscription time in PHP we need three items: the end date from the future in timestamp format, the current date in timestamp format, and a function to convert the difference between the two into something that is actually understandable to a human who's brain can not figure out how many days are within a given thousand seconds. Click here to read on.
Posted by Garren Harland on August 3, 2009, 6:23 pm.
This page discuses how you can trick the PHP function mktime() into returning the Unix timestamp of a date within a specified distance in the future or from the past. To first illustrate this we will use the following example. Click here to read on.
Posted by Garren Harland on August 3, 2009, 10:15 am.
Most programmers go about this task the wrong way. Or rather they only complete half the job, and then wonder why they are not getting the intended results. Usually they do something along the lines of subtracting an old timestamp from the current one, which is actually a step in the right direction, but then they get frustrated when strftime() returns them some date from the 70s. Click here to read on.
Posted by Garren Harland on July 30, 2009, 5:57 pm.
Since the 1st of January 1970 the Unix clock has been ticking, one second at a time. Thanks to this every programmer has one time format from which all the others can be derived. In PHP this timestamp (with the current date, time, etc) can be loaded via the function. Click here to read on.
Posted by Garren Harland on July 30, 2009, 5:12 pm.
Timestamps are great for the following reason. They can be converted into any chosen format without having to do much pre-converting. This saves a lot of time compared to times and dates stored in human readable formats. Click here to read on.
Posted by Garren Harland on July 30, 2009, 4:26 pm.
Using the PHP function date() the current date and time can displayed numerous ways. Here then is a reference guide to help demonstrate these numerous formats and their respective codes. Click here to read on.
Posted by Garren Harland on July 6, 2009, 2:36 pm.
Here is how you find out if the current year is a leap year with PHP. The code date("L") will either return a 0 if it is not currently a leap year, or a 1 if it does happen to be one. Click here to read on. |