SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    php date shows 1970 instead of inserted date

    hi all, i got this default date mysql format. which i wanna call like mm-dd-yyyy. i tried date function but its returning 01/01/1970. here is my code
    PHP Code:
    date("m/d/Y",$this->mevents[$i]->date
    what i m doing wrong?
    All those who wander aren't lost.

  2. #2
    SitePoint Enthusiast Adam Chrapkowski's Avatar
    Join Date
    Sep 2011
    Location
    Poland
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The default MySQL date format is yyyy-mm-dd or unix timestamp. I guess $this->mevents[$i]->date is not an integer (unix timestamp), so the date function defaults to 0 which is 1970-01-01.

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    MySQL's DATE field is Y-m-d. It's DATETIME field is Y-m-d H:i:s.
    If you're trying to take output from a mysql date field and translate it into your format, strtotime the result first, because date()'s second parameter is a timestamp, not a datestring.

    date("m/d/Y",strtotime($this->mevents[$i]->date))

  4. #4
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Thanks Starlion it helped, i think it was string like adam said. though i am using calender on front end. it has format of yyyy-mm-dd, if i swap the mm-dd-yyyy. the values in DB goes as 00-00-0000. why is that?
    All those who wander aren't lost.

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    MySQL's date field can only take in strings in the format Y-m-d.

    If you want to store a date in mysql, you will have to reformat it into that format.

    Example:
    $_POST['date'] is a field coming in from your calendar in m-d-y form.
    PHP Code:
    $date date('Y-m-d',strtotime($_POST['date']))
    $query "INSERT INTO table(`date`) VALUES('".$date."')"
    (Note: This will also sanitize the date, as strtotime will return -1 if the post value is not a time-string.)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •