Jump to content

Archived

This topic is now archived and is closed to further replies.

BadGoat

Date() stopped working

Recommended Posts

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>

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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']));

Share this post


Link to post
Share on other sites
[quote author=BadGoat link=topic=104530.msg416989#msg416989 date=1155757051]
Yep, changing the data type from date to text fixed it. :)

Many thanks!
[/quote]

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

Share this post


Link to post
Share on other sites
[quote author=Barand link=topic=104530.msg417065#msg417065 date=1155765040]
[quote author=BadGoat link=topic=104530.msg416989#msg416989 date=1155757051]
Yep, changing the data type from date to text fixed it. :)

Many thanks!
[/quote]

Bad choice. You should've gone with otuatail's solution.
[/quote]

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote author=Barand link=topic=104530.msg417065#msg417065 date=1155765040]
[quote author=BadGoat link=topic=104530.msg416989#msg416989 date=1155757051]
Yep, changing the data type from date to text fixed it. :)

Many thanks!
[/quote]

Bad choice. You should've gone with otuatail's solution.
[/quote]

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote author=Barand link=topic=104530.msg417082#msg417082 date=1155766224]
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.
[/quote]

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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 ;)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.