php - Empty date and time in mysql table

944

I am inserting date and time in date-time column where I am getting a value only for date and time I am assigning it manually, to make the date-time to match MySQL date-time syntax I did something like :

if($start==NULL && $end==NULL){    
    $start = '01:00:00';
    $end = '02:00:00';    
}

$eve_start = $year."-".$nmonth."-".$date." ".$start;
$eve_end = $year."-".$nmonth."-".$date." ".$end;

Now I am getting year in $year, month in $nmonth, date in $date and $start as my start time and $end as end time.

$eve_start should look like 2012-06-01 01:00:00, also i am getting proper values in place of $year, $nmonth, $date when I printed them individually, but when I printed $eve_start or $eve_end I got only year part printed.

Also in database the corresponding value for date-time inserted as 0000-00-00 00:00:00 where it must be 2012-06-01 01:00:00.

I am getting these values from XML tags, I am importing Google calendar events using XML format and CURL. Now for simple events i get start and end dates with time. But when it is an All day event I dont get time in the xml file, I only get start and end date. Now what i am doing is whenever all day event comes I check for start and end time, if it is null I assign static values to them. I don't have any problems in inserting simple events in my db only prob is with ALL day events

754

Answer

Solution:

you should store time in timestamp format using mktime(). And retrive it using date() functin.

845

Answer

Solution:

You should format this using date() function

$start_dt = date("Y-m-d H:i:s", strtotime($eve_start));
$end_dt = date("Y-m-d H:i:s", strtotime($eve_end ));
808

Answer

Solution:

Look at MySQL DATE_FORMAT.

MySQL store default {-code-5} time as yyyy:mm:dd h:i:s.

Below code will help you to solve issue.

SELECT
      DATE_FORMAT(YOUR_DATE_FIELD,'%Y') AS YOUR_YEAR,
      DATE_FORMAT(YOUR_DATE_FIELD,'%m') AS YOUR_MONTH,
      DATE_FORMAT(YOUR_DATE_FIELD,'%d') AS YOUR_DAY
from your_table

and to store the {-code-5} time field in MySQL you can simply add NOW() mysql function to store current {-code-5} and time.

INSERT INTOdata (date ,{-code-5} ) VALUES('".${-code-5}."',NOW() );

can you please edit your question and add some more detail regarding problem.

  1. from where that three variables are coming from.
  2. what value you are assigning to those three.
  3. are you getting those three from MySQL query if yes then my above solution will solve it.

if its still not solving issue then the problem may not be in the script, but in the browser cache

run the code and see the outcomes that will helps.

687

Answer

Solution:

  1. PHP side: I tried your same code and PHP prints date-times correctly for me.

    http://codepad.org/mKmNM9Yu

  2. Mysql side: Make sure the column type is 'datetime'. And makesure you have assigned the date variable correctly in the insert query.

People are also looking for solutions to the problem: php - it wont sum up, what is wrong with my code?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.