how to get datetime object with value 0 or null?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kc11
Forum Commoner
Posts: 73
Joined: Mon Sep 27, 2010 3:26 pm

how to get datetime object with value 0 or null?

Post by kc11 »

Hi,

I would like to get a dateTime object with the value of 0 or null.

I've tried:

Code: Select all


 $nulldate=new DateTime('0000-00-00');
       $nd= $nulldate->format('Y-m-d');  
       
       echo $nd;

This gives me:
-0001-11-30
Any ideas?

Thanks in advance,

KC
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to get datetime object with value 0 or null?

Post by Celauran »

What are you trying to accomplish here?
kc11
Forum Commoner
Posts: 73
Joined: Mon Sep 27, 2010 3:26 pm

Re: how to get datetime object with value 0 or null?

Post by kc11 »

I want to load this into a mysql db. I am using rebeanphp orm. which does not ( as far as I can tell ) have a way to explicitly set the mysql field type , so by passing it a datetime object, I can set the mysql field to type date.

Thanks,
Last edited by kc11 on Fri Mar 16, 2012 12:43 pm, edited 1 time in total.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to get datetime object with value 0 or null?

Post by Celauran »

Just leave it NULL.
kc11
Forum Commoner
Posts: 73
Joined: Mon Sep 27, 2010 3:26 pm

Re: how to get datetime object with value 0 or null?

Post by kc11 »

Code: Select all

$nulldate=new DateTime(NULL);
$nd= $nulldate->format('Y-m-d');
echo $nd;
 


returns the current date
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to get datetime object with value 0 or null?

Post by Celauran »

I mean if you don't want a date in the database, let the field be NULL.
kc11
Forum Commoner
Posts: 73
Joined: Mon Sep 27, 2010 3:26 pm

Re: how to get datetime object with value 0 or null?

Post by kc11 »

I'm not sure how to do that. Is there a sql command for that?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to get datetime object with value 0 or null?

Post by Celauran »

Code: Select all

ALTER TABLE table_name CHANGE field_name field_name DATETIME NULL
kc11
Forum Commoner
Posts: 73
Joined: Mon Sep 27, 2010 3:26 pm

Re: how to get datetime object with value 0 or null?

Post by kc11 »

Thank you, I'll check it out and open a new thread if I run into problems.

Best Regards,

KC
Post Reply