Question

I'm using EasyPHP and Codeigniter to develop my blog, and since I'm, and probably most people, ordering comments, posts, etc. by date/time it was posted, I have some trouble with sorting it, because EasyPHP somehow gets the time wrong, I mean previous entry was 2013-11-09 03:11:40 and most recently posted is 2013-11-09 03:11:20. To get the time before I make a query, I do:

date('Y-m-d h:m:s');

Sample of code that's executed:

public function new_comment()
    {
        $text = $this->input->post('text');
        $p_id = $this->input->post('p_id');
        $u_id = $this->input->post('u_id');
        $time = date('Y-m-d h:m:s');
        $username = $this->input->post('username');

        $data = array(
            'user_id' => $u_id,
            'post_id' => $p_id,
            'text' => $text,
            'time' => $time,    
            'username' => $username
        );

        if($this->db->insert('comments', $data))
        {
            $back['sql'] = 'Query successful';
            $back['next'] = '1';
            $back['c_id'] = $this->db->insert_id();
            $back['time'] = $time;
        }
        else
        {
            $back['sql'] = 'Something went wrong';
            $back['next'] = 0;
        }

        print json_encode($back);
    }

I doubt this is a code related issue, possibly this is just localhost being a derp. Why is this a problem? I'm testing some fancy AJAX loading comments without refreshing and I can't test it because date is bad. Anyone had this issue?

Was it helpful?

Solution

You are using "m" twice, the first time it should mean "month", the second time "minutes" - which it does not.

The correct format string is "Y-m-d H:i:s". The format identifier for "minutes" is "i".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top