Jump to content


Photo

Date() stopped working


  • Please log in to reply
14 replies to this topic

#1 BadGoat

BadGoat
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 16 August 2006 - 07:04 PM

Hello!

I wrote a little script which uses the MySQL date datatype which does not seem to be working at the moment. It worked just fine when I wrote it on my local machine, but it is no longer working now that it's up on the host. Can someone take a look and see if I did something wrong, or if it can be optimized to work in most situations?

$sip=$_POST['sip'];
$eip=$_POST['eip'];
$nic=$_POST['nic'];
$city_id=$_POST['city_id'];
$country_id=$_POST['country_id'];
$company_id=$_POST['company_id'];
$contact=$_POST['contact'];
$phone=$_POST['phone'];
$e1=$_POST['e1'];
$e2=$_POST['e2'];
$e_ver=$_POST['e_ver'];
$updated= date('M j Y');

$sqlquery = "INSERT INTO ip_block VALUES('','$sip','$eip','$nic','$city_id','$country_id','$company_id','$contact','$phone','$e1','$e2','$e_ver','$updated')";
$queryresult = mysql_query($sqlquery) or die(" Could not execute mysql query !");
$sqlquery = "SELECT * from ip";


and then where the date is echoed:

<tr>
    <td class="selcol1">Date Updated</td>
    <td class="selcol2">' .$updated. '</td>
</tr>

#2 karthikeyan_coder

karthikeyan_coder
  • Members
  • PipPipPip
  • Advanced Member
  • 201 posts

Posted 16 August 2006 - 07:10 PM

it is showing date well in my localhost:2112 too.. can you show me the out put in your server as well as in localhost?



www.karthi.us

#3 BadGoat

BadGoat
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 16 August 2006 - 07:15 PM

Well, on the page which echoes the date updated it does show the correct date, but when I check in PHPMYADMIN it shows 0000-00-00.

#4 karthikeyan_coder

karthikeyan_coder
  • Members
  • PipPipPip
  • Advanced Member
  • 201 posts

Posted 16 August 2006 - 07:19 PM

Ok, change the field's type to "TEXT", which is going to save $updated.. i think it is in "DATE" or datetime format... just Alter that field to TEXT... coz date('M j Y'); will return "Aug 16 2006" which is having some alphabetic chars... so change that field's type to TEXT.
www.karthi.us

#5 otuatail

otuatail
  • Members
  • PipPipPip
  • Advanced Member
  • 960 posts

Posted 16 August 2006 - 07:27 PM

If you want to store a date in the database it has to be 'YYYY-MM-DD' you can display it in any format using strtotime() 

This is mine.
$datetime = date('D d-M-Y', strtotime($row['Date']));

#6 BadGoat

BadGoat
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 16 August 2006 - 07:37 PM

Yep, changing the data type from date to text fixed it. :)

Many thanks!

#7 karthikeyan_coder

karthikeyan_coder
  • Members
  • PipPipPip
  • Advanced Member
  • 201 posts

Posted 16 August 2006 - 08:30 PM

ok fine :)

http://TopLancers.com
www.karthi.us

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 16 August 2006 - 09:50 PM

Yep, changing the data type from date to text fixed it. :)

Many thanks!


Bad choice. You should've gone with otuatail's solution.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 16 August 2006 - 09:52 PM

Yep, changing the data type from date to text fixed it. :)

Many thanks!


Bad choice. You should've gone with otuatail's solution.


Yes, this is because you will run into troubles if/when you need to perform things such as sorting on the date.

#10 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 16 August 2006 - 10:00 PM

And the implication of that is you can't select records "..WHERE date > '$somevalue'",  or '<'.

You also deny yourself the ability to use the dozens of MySql date/time functions.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#11 karthikeyan_coder

karthikeyan_coder
  • Members
  • PipPipPip
  • Advanced Member
  • 201 posts

Posted 16 August 2006 - 10:04 PM

Yep, changing the data type from date to text fixed it. :)

Many thanks!


Bad choice. You should've gone with otuatail's solution.


I said a solution just for inserting date... and he likes to handle date('M j Y'); will return "Aug 16 2006"... for inserting these kind of date format "TEXT" field is easy.
www.karthi.us

#12 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 16 August 2006 - 10:10 PM

But what's the point of storing a date whose sole purpose is to look pretty. Dates should be stored for functionality. The format can be chosen as you please when you query the table or output it.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#13 karthikeyan_coder

karthikeyan_coder
  • Members
  • PipPipPip
  • Advanced Member
  • 201 posts

Posted 16 August 2006 - 10:16 PM

But what's the point of storing a date whose sole purpose is to look pretty. Dates should be stored for functionality. The format can be chosen as you please when you query the table or output it.



yes, agreed. but my answer will be easy for beginners. it is just inserting and showing a date. ;)
www.karthi.us

#14 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 16 August 2006 - 10:23 PM

It's just as easy to insert a date in 'Y-m-d' format as it is to use 'M j Y'.

Why should they be given poor advice just because they are beginners?
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#15 karthikeyan_coder

karthikeyan_coder
  • Members
  • PipPipPip
  • Advanced Member
  • 201 posts

Posted 16 August 2006 - 10:31 PM

he didnt mentioned any qurries like WHERE `date` > ... like that... he just asked to insert. thats it.  i said just for a quick solution not a geeky solution. lol ;)
www.karthi.us




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users