Jump to content

Date time format corrupted online


Wickham

Recommended Posts

This code from Stefan's tutorial for sessions for showing date and time placed above the doctype:-

<?php session_start();

$_SESSION['time'] = date(DATE_RFC822);

?>

 

and this as the body output:-

<?php

echo "Welcome on " . $_SESSION['time'];

?>

 

shows this result when online in IE7 and Firefox:-

 

SunPMGMTE_RFebruaryC850

 

but correctly this when viewed in WampServer:-

 

Sunday, 01-Feb-09 14:34:44 GMT

 

Any ideas why it's corrupted online?

 

Edit: I've reduced it to the minimum code here:-

http://w ww.wickham43.com/forumposts/phpdatetime.php

 

which is still corrupted online in IE7 and in Firefox online, but OK on localhost in Firefox.

 

Edit again: this is ridiculous! I found this http://uk3.php.net/date Kenneth Kin Lun 2 Oct 2008

 

date(DATE_RFC822) and date(DATE_RFC2822) both work. note that RFC 822 is obsoleted by RFC 2822. The main difference is the year being 08 in RFC 822 and is 2008 in RFC 2822.

 

To use date(DATE_RFC2822), a short form is date('r').

 

RFC2822 did not work for me but 'r' did work (I've not edited my own url above so that you can still check if it's corrupted for you.)

Edited by Wickham
Link to comment
Share on other sites

I just worked with php's date() function recently when I relaunched the blog section of my website... I did find it a bit confusing, especially since I wanted to save dates in a database using MySQL's "timestamp" data type (which looks like 0000-00-00 00:00:00, or year-month-day hour:minutes:seconds), and sometimes the user would enter the date in a 00/00/00 format.

 

Here are the functions I am using: (be aware that I'm still getting started with PHP, so I'd appreciate comments if there is anything anyone has to add)

 

   // convert current time to timestamp
   // input: none
   // returns now() in timestamp (0000-00-00 00:00:00) format
   function getTime()
   {
       return date("Y-m-d H:i:s", time());
   }

   // converts timestamp to readable format
   // input: timestamp, format
   // output: string - human readable date/time in specified format
   function convertTimestampToReadable($timestamp, $format)
   {
       $readable = strtotime($timestamp);
       return date($format, $readable);
   }

   // converts date (00/00/00) to timestamp
   // input: date, delimiting character(s)
   // output: string in timestamp format
   function convertDateToTimestamp($date, $delimiter)
   {
       $date = explode($delimiter, $date);
       return '20'.$date[2].'-'.$date[0].'-'.$date[1].' 00:00:00';
   }

   // convert timestamp to date (00/00/00)
   // input: timestamp, delimiter
   // output: string in date format
   function convertTimestampToDate($timestamp, $delimiter)
   {
       $date = substr($timestamp, 2, 8);
       $date = explode("-", $date);
       return $date[1] . $delimiter . $date[2] . $delimiter . $date[0];
   }

Link to comment
Share on other sites

I found that mysql doesnt like the time stamp, but if you use two feilds in the mysql database, one for time and one for date it works ok.

 

set the feild type to date for date and time for time.

 

then use:

 

<?php

$date = date("Y/m/d");

$time = date("H:i:s");

mysql_query("INSERT INTO `blogs` (date, time) VALUES ('$date', '$time')");

?>

 

this will time stamp with server time.

 

if you want them to input the time/date themselves use 3 input boxes (year month day)

 

<?php

$seperator = "/";

$year = $_POST['year'];

$month = $_POST['month'];

$day = $_POST['day'];

$date = $year.$seperator.$month.$seperator.$day;

?>

 

check value is a number:

 

if (is_int($year)) {

 

}

 

check amount of digits:

 

$yearcheck = strlen($year);

if ($yearcheck==4) {

 

}

Link to comment
Share on other sites

I've had no trouble with TIMESTAMP in MySQL either.

 

sjhwebdesign, you offered separate date and time codes; I've had to edit them to be in a session and they work as shown here:-

http://w'>http://w ww.wickham43.com/forumposts/phpdatetime2.php

 

My original code was supposed to be combined and a standard code but it didn't work online, only on WampServer, which seemed crazy. I found that the short combined version date('r') did work online as shown here:-

http://w ww.wickham43.com/forumposts/phpdatetime3.php

 

I'm still mystified why the proper combined versions date(DATE_RFC822) or date(DATE_RFC2822) don't work online. Why are they shown on tutorials? Edit: my hosting service has PHP 5.2.5 so it can't be that it's an old version.

Edited by Wickham
Link to comment
Share on other sites

when ive used time stamp in mysql and used the php date function ive ended up with 00/00/00 00:00:00 or there abouts.

 

That may be something that you are doing wrong in your code -- doesn't necessarily mean that PHP/MySQL is wrong. As I said above, I've been using the functions I posted live on my blog (http://ww w.falkencreativ e.com/journal.php [remove spaces]) to store/retrieve dates/times and I haven't had any trouble at all.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...