Format DATETIME column using PHP after printing - php

Hey, I am using DATETIME for my column for my database. I am trying to find the best way to format this date held in the row using PHP. I tried using date() but that would just print the current date. My row looks something like:
$row['date_added']
Any ideas?

A couple options.
Use MySQL's DATE_FORMAT function to format it.
Use MySQL's UNIX_TIMESTAMP function to get a timestamp PHP's date() function can play with.
Run the MySQL DATETIME through PHP's strtotime() function to get a timestamp PHP's date() function can play with.
As an example of #3:
print date('<format string>', strtotime($row['date_added']));
Just follow the documentation on date() to construct a format string to your liking and swap it in.

Related

Trying to convert mysql timestamp to time using strtotime

strtotime($_SESSION['starttime'])
I tried to convert the mysql time stored as $_SESSION['starttime'] of value 1422094831 but I want to convert the resulting to time using strtotime but returns no value. What could be wrong?
strtotime() is used to create a timestamp, not convert it to a date. If your session variable stores a timestamp, try using the date() function to convert it to a more readable format.
echo date("Y-m-d", $yourTimestamp); //Will print YYYY-MM-DD format date
The PHP doc will help you using the date() function correctly.
If that's not what you want to do then please provide more information in your question, from what I understand you already have a timestamp.

codeigniter get timestamp from database and format

I have a MYSQL table column of type timestamp and using CURRENT_TIMESTAMP as default when a new record is added.
The date field in my database table looks like this 2011-08-16 12:09:25 How is it possible to format that into MM/DD/YY to display on my site? I tried some functions from the date helper but I get errors.
note: I'm trying to figure out how to use Codeigniter functions for this, if possible.
In the CI manual there are examples with $timestamp = '1140153693'; but my database timestamp is a different format and get errors.
To display a timestamp, just use the PHP Date class. But since MySQL outputs timestamps as a string not an INT, you'll have to first convert the timestamp string to a timestamp INT using PHP's strtotime function. The code looks like this:
echo date("m/d/y",strtotime($timestamp));
Date
http://www.php.net/manual/en/function.date.php
StrToTime
http://php.net/manual/en/function.strtotime.php
Sorry, I didn't properly explain the issue but solved it.
In order to use the Codeigniter date functions I had to convert the mysql ISO 8601 date format to a 32-bit integer.
I had to do the following
$unix = mysql_to_unix($row->message_date); //conversion to string
$datestring = "%m/%d/%Y";
echo mdate($datestring, $unix);
I was using the ISO 8601 format and getting errors.

Abbreviated string from MySQL 'time' field type in PHP

I have a MySQL field which stores times using the 'time' field type, such as '12:30:00' i.e. in the HH:MM:SS format.
In my PHP app I want to convert such HH:MM:SS data into a more readable string, such as '12.30pm'.
Is there any function for doing this, or will I have to do it by other means?
Many thanks!
I don't have a machine to test it on at the moment, but I would look at: strtotime and date. I believe that the following should be what you want:
$print_time = date("h.ia", strtotime($sql_time));
Where $print_time is the pretty printable time string and $sql_time is the SQL timestamp string.
If you don't mind changing you MySQL code you can just use MySQL's DATE_FORMAT() on the fetc:
SELECT DATE_FORMAT(table_name.time_col, '%l:%i %p') AS myFriendlyDate;
Why not use the datetime functions built into MySQL?
DATE_FORMAT will allow you to manipulated it however you want.
Or any of the other functions may help you with it ...
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

PHP - Inserting date into datetime field

I have used a datetime field in my database for storing dates, whats the proper way to insert say todays date into that field using PHP?
Cheers,
Don't use PHP at all, but the database's built-in function for that.
Assuming you're using mySQL, the function's name is NOW().
11.7. Date and Time functions in the mySQL manual
i think you can use the php date() function
INSERT INTO table (`date`)VALUES (NOW());
Use the PHP function time() instead and store the value as a normal integer - that gives you much more possibilities.
As everyone else has said, using the MySQL function NOW() would probably be the best bet for a DATETIME column. MySQL also has many date and time functions available.
Personally, I do what Ivarska suggested and use an INT data type with PHP's time() or MySQL's UNIX_TIMESTAMP(). It gives the date in the GMT standard and it will port over easily.

Is there any PHP function to format time (not date)?

I have a mysql table field set as time type which stores data in the HH:mm:ss format. So when I list the data, it prints as, for example, 16:30:00. But I want to display hh:mm part only (not the seconds).
In case of datetime types, I can do date('H:i', '2010-03-16 16:30:00'). I mean I can retrieve any part. I wonder if there is any similar way like this for time only fields??
Please see, I can manipulate the time string to get rid of seconds in time part using str_replace, explode etc, I just wonder if there is any standard function there which I am not aware of.
If you want to do this with PHP, you'd have to get a timestamp from the time first, e.g.
echo date('H:i', strtotime('16:30:00'));
will output 16:30. Strtotime will assume the time is for the current date then.
You can let MySQL return the data in the format you want using Date_Format()
edit: as fireeyedboy pointed out there's also a TIME_FORMAT() function.
Another approach is:
SELECT UNIX_TIMESTAMP(time_field) AS tstamp FROM table;
Then you can use PHP's date() function to format it.
$time = date('H:i', $tstamp);

Categories