Archive

Posts Tagged ‘timestamp’

XML Sitemap Date Format In PHP

April 3rd, 2009 No comments

To format the current timestamp in W3C Datetime encoding (as used in sitemap.xml files) use the following parameters.

echo date('Y-m-dTH:i:sP', time());

As of PHP5 you can also use the c format character to print the exact same string.

echo date('c',time());

These would both print out the following:

2009-04-03T11:49:00+01:00

Categories: PHP Tags: , , , , ,

Convert A Date Into Timestamp In JavaScript

February 27th, 2009 1 comment

I have previously talked about using PHP to convert from a date to a time, but what about doing the same in JavaScript?

To get the unix timestamp using JavaScript you need to use the getTime() function of the build in Date object. As this returns the number of milliseconds then we must divide the number by 1000 and round it in order to get the timestamp in seconds.

Math.round(new Date().getTime()/1000);

To convert a date into a timestamp we need to use the UTC() function of the Date object. This function takes 3 required parameters and 4 optional parameters. The 3 required parameters are the year, month and day, in that order. The optional 4 parameters are the hours, minutes, seconds and milliseconds of the time, in that order.

To create the time do something like this:

var datum = new Date(Date.UTC('2009','01','13','23','31','30'));
return datum.getTime()/1000;

This prints out 1234567890. Here is a function to simplify things:

function toTimestamp(year,month,day,hour,minute,second){
 var datum = new Date(Date.UTC(year,month-1,day,hour,minute,second));
 return datum.getTime()/1000;
}

Notice that when adding in the month you need to minus the value by 1. This is because the function requires a month value between 0 and 11. However, there is an easier way of getting the timestamp by using the parse() function. This function returns the timestamp in milliseconds, so we need to divide it by 1000 in order to get the timestamp.

function toTimestamp(strDate){
 var datum = Date.parse(strDate);
 return datum/1000;
}

This can be run by using the following, not that the date must be month/day when writing the date like this.

alert(toTimestamp('02/13/2009 23:31:30'));

Or even this:

alert(toTimestamp('2009 02 13 23:31:30'));

Convert Time Into Timestamp Or Timestamp Into Time

May 10th, 2008 1 comment

The easiest (and most reliable) way to store the time in a database table is with a timestamp. It is also the most convenient way of working out time scales as you don’t have to do calculations in base 60. In MySQL this is accomplished by the UNIXTIME() function, which can be reversed by using another MySQL function called FROM_UNIXTIME().

However, you can sometimes be left with timestamps in your code and the task of trying to figure out what to do with them.

The first problem is trying to convert a timestamp into a date. So here is a PHP function that does this.

function timestamp($t = null){
 if($t == null){
  $t = time();
 }
 return date('Y-m-d H:i:s', $t);
}

And if you ever have a the opposite problem then here is a PHP function that converts a date string into a timestamp. At the moment the string needs to be in the format YYYY-MM-DD HH:MM:SS, which is what the previous function produced. This isn’t too difficult to change, just alter the parameters and order of the explode(‘ ‘, $str).

function convert_datetime($str) {
 
 list($date, $time) = explode(' ', $str);
 list($year, $month, $day) = explode('-', $date);
 list($hour, $minute, $second) = explode(':', $time);
 
 $timestamp = mktime($hour, $minute, $second, $month, $day, $year);
 
 return $timestamp;
}

Here is an example of the functions in use.

echo timestamp(convert_datetime('2008-05-10 20:56:00')). ' '. convert_datetime('2008-05-10 20:56:00') . ' 1210467360';

Categories: PHP Tags: , , , ,

Some Common Timestamp Intervals

May 2nd, 2008 No comments

Using timestamps is quite a common practise, but converting them into "real life" times can be a little hard. So here are some common time intervals that you might need.

  1. 1: 1 second
  2. 60: 1 minute
  3. 300: 5 minutes
  4. 600: 10 minutes
  5. 2700: 45 minutes
  6. 3600: 1 hour
  7. 54000: 15 hours
  8. 86400: 1 day
  9. 18400: 6 days
  10. 604800: 1 week
  11. 1814400: 3 weeks
  12. 2419200: 1 month
  13. 26611200: 11 months
  14. 14515200: 6 months
  15. 29030400: 1 year
Categories: General Tags: , ,