Aug 23, 2010

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'); 

No comments: