Aug 23, 2010

Handling date with PHP (Third Part)

Converting from one format to Another

"I have got a date in DD / MM / YYYY but I need it in MM-DD-YYYY. Help!"

How do we help this person? How do we help this person? Well, let's analyze the problem a bit. Well, let's Analyze the problem a bit. Each date consists of three components: the day, the month and the year. Each date consiste of three components: the day, the month and the Year. So, essentially what we need to do is reorder it and use hyphens instead of slashes. So, essentially what we need to do is reorder it and instead use hyphens or slashes. There are a few ways we can accomplish this. There are a few ways we can 'accomplish this. Let's first try splitting it up. Let's first try splitting it up. We've got a function called explode() for that. We've got a working Called explode () for that.
 $oldDate = '18/08/2009'; // DD/MM/YYYY $ OldDate = '18 / 08/2009 ', / / DD / MM / YYYY

$parts = explode('/', $oldDate); $ Parts = explode ('/', $ oldDate);

/** / **
 * Now we have: * Now we have:
 *   $parts[0]: the day * $ Parts [0]: the day
 *   $parts[1]: the month * $ Parts [1]: the month
 *   $parts[2]: the year * $ Parts [2]: the Year
 * We could also have done: * We Could Also Have done:
 *   list($day, $month, $year) = explode('/', $oldDate); * List ($ day, $ month, $ year) = explode ('/', $ oldDate);
 */ * /

$newDate = "{$parts[1]}-{$parts[0]}-{$parts[2]}"; // MM-DD-YYYY $ NewDate = "($ parts [1 ]}-{$ parts [0 ]}-{$ parts [2]) '/ / YYYY-MM-DD

echo $newDate; // 08-18-2009 echo $ newDate / / 08-18-2009 
Seems pretty nice to this work. We can also use something called regular expressions. We Can Also Called something use regular expressions. This will essentially be a one-liner: Will this be essentially a one-liner:
$oldDate = '18/08/2009'; $ OldDate = '18 / 08/2009 ';
$newDate = preg_replace('#^(\d{2})/(\d{2})/(\d{4})$#', '$2-$1-$3', $oldDate); $ NewDate = preg_replace ('#^( \ d (2)) / (\ d (2)) / (\ d (4 })$#', '$ 2 - $ 1 - $ 3' $ oldDate); 
I am not going to explain regular expressions in this tutorial. You may check out this regex tutorial for that. You May check out this tutorial regex for that.

Any other ways? Any Other Ways? Yes, there is! Yes, there is! As it usually happens to be with programming, there are many ways to do just one thing. As it happens to be with programming Usually, there are many ways to do just one thing. Assuming we have already split up our date in three variables $day , $month and $year we can use a function called mktime() for generating a Unix timestamp and then use date() to format it in this way: Assuming we split up our possessions Already date in three variables $ day, $ month and $ year Can we use a function Called mktime () for generating a Unix timestamp and then use date () to format it in this way:
 $timestamp = mktime(0, 0, 0, $month, $day, $year); $ Timestamp = mktime (0, 0, 0, $ month, $ day, $ year);
echo date('md-Y', $timestamp); echo date ('md-Y', $ timestamp); 
Yet another way is using strtotime () to convert a formatted string Writing a Unix timestamp. EXCEPT, strtotime() does not recognize DD/MM/YYYY as a valid date format, so it returns false. EXCEPT, strtotime () Does Not Recognize DD / MM / YYYY as a valid date format, so it returns false. There are also problems with ambiguity. Also there are problems with ambiguity. Consider the string 07-08-2009. Consider the string 07/08/2009. Is that the 7th of August, or is it the 8th of July? Is that the 7th of August, or is it the 8th of July? It will be the former. Will it be the performer.


Handling date with PHP (Second Part)

Getting Time And Date With PHP
My computer's local time is 9:34:08, so That is also the output. Now we'll try to change the timezone: Now we'll try to change the time zone:
date_default_timezone_set ('America / New_York');
echo date('H:i:s'); echo date ('H: i: s');  
What if You Have users from multiple time zones? Still simple! Still simple! Ask them what their timezone is and set it accordingly. Ask Them what their time zone and set it accordingly. The function timezone_identifiers_list() gives you an array of all the timezone identifiers, so you can use that to generate a drop down list they can choose from. The function timezone_identifiers_list () gives you an array of all the timezone identifiers, so That You Can Use to Generate a drop down list Can They choose from. You can also try out geolocating, but that is beyond the scope of this tutorial. You Can Also try out geolocating, but That is beyond the scope of this tutorial.

Finally, PHP has a function called gmdate() , which always formats the date in GMT. Finally, Has a PHP function Called gmdate () , Which always formats the date in GMT. If you know the offset in seconds you can do like: If you know the offset in seconds you can do like:
 gmdate ('H: i: s', time () + $ offset); 
So You Might Do
 gmdate ('H: i: s', time () + 3600); 
for an offset of one hour. However, this requires you to take DST into consideration. However, this Requires you to take DST Writing considération. Not all countries observe DST, and some do it differently than others. Not all countries Observe DST, and Some Do It Differently Than others. My recommendation would be to use PHP's built-in support for timezones. My Recommendation Would be to use PHP's built-in support for time zones.

Handling date with PHP (First Part)

There are many topics on the forums That go again, many topics in loss or people are having trouble with. One of these problem areas are how to handle dates, convert them to a different format, timezone issues, etc. This tutorial will attempt to address many of the commonest problems related to date and time issues. One of These problem areas are how to handle dates, convert theme to a differential format, time zone issues, etc. This tutorial Will Attempt to address many of the commonest problems related to date and time issues.

Fault date and time in a database.

Before Covering how you handle dates in PHP, I want to talk a little about how you store dates in a database Should. Specifically I'm going to talk about MySQL because that is what I've got the most experience with. Specifically I'm going to talk about MySQL Because That Is What I've got the experience with musts. The other DBMS have also got facilities for working with date and time information, so you can check out the manual for these. The other possessions Also got DBMS facilities for working with date and time information, so you-can check out the manual for thesis.

Many people "invent" their own way of storing dates in the database, and this inevitably gives them some trouble later on. Many people "invent" Their Own Way or failure dates in the database, and this inevitably gives Them Some trouble later on. They may for instance have a VARCHAR field type and store dates in a DD/MM/YYYY format. They for instance May Have a VARCHAR field type and store dates in a DD / MM / YYYY format. This is the wrong way to do it. This is the wrong way to do it.
There are a number of different ways you can store dates properly in a database. There are a number of different ways of You Can Properly store dates in a database. One way is to have an integer field and store a Unix timestamp. One way is to store possessions an integer field and a Unix timestamp. I don't like this approach. I do not like this approach. It makes you unable to use MySQL's date and time functions, and there are the potential issues with the Y2K38 problem . It makes you Unable to use MySQL's date and time functions, and there are the potential issues with the Y2K38 problem .
MySQL has a datatype called TIMESTAMP. Called MySQL Has A datatype TIMESTAMP. This displays the date and time in an ISO 8601 format, ie from the most significant part (year) to the least significant part (second). This displays the date and time in an ISO 8601 format, ie from the must significant part (year) to the least significant part (second). An example could be 2009-08-18 9:17:21 . An example Could Be 18/08/2009 9:17:21. The extra formatting isn't actually necessary when typing it in, so 2009081891721 would be the same thing. The additional formatting isn't Actually Necessary When typing it in, so 2009081891721 Would Be the Same Thing.
There is also a datatype called DATETIME. There is Also Called a data type DATETIME. First looking at it, it seems identical to the TIMESTAMP, but that is not the case. First looking at it, it Seems Identical To The TIMESTAMP, but That is not the case. First of all, TIMESTAMP still have issues with the Y2K38 thing, so it can only store dates that can be represented with a 32-bit unsigned integer starting from the Unix epoch (1970-01-01 00:00:00). First of all, TIMESTAMP Still Have Issues With The Y2K38 thing, So It Can only store dates That Can Be Represented with a 32-bit unsigned integer starting from the Unix epoch (1/1/1970 00:00:00). This gives an upper limit 2038-01-19 03:14:07. This gives an upper limit 01/19/2038 3:14:07. DATETIME does not have this problem; it can store dates from 1000-01-01 to 9999-12-31. DATETIME Does Not Have this problem, it dates from 01/01/1000 to Can store 9999-12-31. Finally, there are some datatypes called DATE and TIME. Finally, there are Some data types DATE and TIME Called. These are for if you just need either the date or time part only. These are for if you just need Either the date or time part only.
My recommendation is to use either DATETIME, DATE or TIME depending on your needs. My Recommendation is to use Either DATETIME, DATE or TIME Depends on Your Needs. Actually, there is a YEAR type as well. Actually, there is a YEAR type as well. Using MySQL's built-in datatypes for date and time storage allows you to use its date and time functions . Using MySQL's built-in data types for date and time storage Allows you to use ITS date and time functions .
I'll not be using databases for the remainder of this tutorial. I'll not be using databases for the Remainder of this tutorial. The examples will assume you already have some string containing some date. Will the examples Assuming you Already Have some string containing Some date.

Time Zones

This is a very frequent problem. This is a very frequent problem. When using the date() function in PHP it is always using the server time for generating date and time stamps. When using the date () function in PHP it is always using the server time for generating date and time stamps. Unless you're in a different timezone than your server, this is not a problem. Unless you're in a time zone differential Than your server, this is not a problem. I have got a VPS (Virtual Private Server) in England, but I live in Denmark. I have got a VPS (Virtual Private Server) in England, but I live in Denmark. That is a time difference of one hour. That is a time difference of one hour. How do I get PHP to display the right time for me? How do I get PHP to display the right time for me?
Because I am the server admin, I can just change the server's clock (and that is actually what I've done). Because I am the server admin, I Can just change the server's clock (and That Is Actually what I've done). Most people cannot just do that, so we'll have to work out some alternative solutions to this problem. Most people just can not do that, so we'll Have to work out alternative solutions to this problem Some.
If you've got access to php.ini (or can change its directives using an Apache .htaccess file) you can change the date.timezone directive (actually, this needs to be set to something if you don't want errors from PHP). If you've got access to php.ini (or Can change ITS using an Apache directives. Htaccess file) Can you change the date.timezone directive (actually, this Needs to be set to something if you do not because errors from PHP ). Otherwise you can use the date_default_timezone_set() function. Otherwise you-can use the date_default_timezone_set () function. The manual has a list of valid timezones you can use for these two things. The manual Has a list of valid timezones you-can-use hypothesis for two things. Because I am in Pakistan, I would choose Asia. 
 Let's try it out:

echo date ("H: i: s');