How to get time with month name,date and hour? - php

i have used Now() and it stores something like "2017-01-10 19:28:58" in database which is the current time of user's device.
But i want it like January 10 at 7:28pm . how to do it in simple way. please help

You can use PHP date function with its formatting options
<?php
echo date("F d \a\t g:ia");
If you want more detailed formatting please visit the PHP manual here. You can find everything you need with detailed examples.

you could use the PHP date function
date(F d \a\t\ g:ia);

Your post is ambiguous, but it sounds like you are referring to the SQL NOW() function. The format of the data stored could be a DATETIME or TIMESTAMP or a combination of DATE and TIME columns. The format you get when you retrieve this value from the database depends on that data storage format, your query, and how the value is dealt with when it gets into PHP.
If you want to reformat it using SQL, consider a function like DATE_FORMAT. Assuming your column with the date is called my_column, here's a sample query.
SELECT DATE_FORMAT(my_column, '%M %e at %l:%i%p');
EDIT: you can also use DATE_FORMAT on the result of the NOW() function:
SELECT DATE_FORMAT(NOW(), '%M %e at %l:%i%p');
You might have to tweak the second parameter to get the date format you want.
If your date is stored as a string (VARCHAR or whatever) in your database, then you would need to convert it to a timestamp or datetime first and then use the PHP date function to output the variant you want. Assuming $row is a record from your data table:
$date_string = $row["my_column"];
$stamp = strtotime($date_string); // NOTE that this will assume some timezone
if (!$stamp) {
die("Could not create a timestamp from the date");
}
echo date("F j \a\t g:ia);
You could also use PHP'S DateTime functions which are more modern, if somewhat verbose in usage.

Related

How to select date and time from oracle date field using php date function

How to take the time from date stored as 12/25/2012 5:12:05 AM .
date('l F j, Y, g:i a',strtotime($last_login_details[FL_DATETIME]));
This above function returned time as 12:00 am which should return 5:12AM.
FL_DATETIME has datatype DATE.
On database, the value is being stored like this :
12/25/2012 5:12:05 AM
According to the docs - http://docs.oracle.com/cd/B19306_01/server.102/b14220/datatype.htm#i1847 -
For input and output of dates, the standard Oracle date format is DD-MON-YY
That is most likely why $last_login_details[FL_DATETIME] is echoing 25-DEC-12
Try changing your query using TO_CHAR()
SELECT TO_CHAR(FL_DATETIME, 'MM/DD/YYYY HH24:MI:SS A.M.') AS FL_DATETIME ...
see http://infolab.stanford.edu/~ullman/fcdb/oracle/or-time.html#date format
Solved my problem by :
SELECT TO_CHAR(FL_DATETIME, 'DD.MM.YYYY:HH24:MI:SS') FROM "FMS_LOG"
First of all, in my opinion, you should be storing all dates as unix timestamps. This makes it lot easier for you to do searches against times, and removes any inconsistencies that may arise from date string manipulation.
Second, I tested your code; it looks to be OK from what I can tell. Echo out what you are getting in the $last_login_details[FL_DATETIME] variable. The issue may lie in the variable assignment, and not the date string manipulation.
Hope that helps!

Formatting datetime

i am trying to format a datetime which comes fromt he database in the format of
2012-06-11 21:39:54
However i want it to display in the format of June 11
How can do this?
Thanks
echo date('M d', strtotime('2012-06-11 21:39:54'));
Output
You can also use DateTime object.
$date = new DateTime($yourString);
$date->format($yourFOrmat);
I think that it would be the best way because DateTime is really more powerful than timestamp and date/strtotime functions.
From the code I gave above you can add functionalities like modifying dates, iterate over the time, compare 2 dates without functions like str_to_time...
$date->modify('+1 day');//the day after for example
foreach(new DatePeriod($date,new DateInterval('PT1M'),10){
$date->format($yourFormat);//iterate each minute
}
and so on
PHP manual gives an excellent documentation about using Date/Time functions. Basically you will need a combination of two functions: strtotime() and date().
strtotime() will convert your date into Unix timestamp which can be supplied to date() as second argument.
The format of date you will need is: M d.
Alternative: In addition you could also try the MYSQL counterpart which won't require conversion to UNIX timestamp. It is documented here. Assuming you are using date as your Datetime field, you will need something like this,
SELECT id,..,DATE_FORMAT(`date`, '%M %d') as f_date FROM table
For formatting date using php, you need to pass timestamp of date
and format specifiers as arguments into date function .
Eg echo date('M d',strtotime('2012-06-11 21:39:54'));

format mysql timestamp using php

i have column named postDate defined as timestamp.
when i print it directly:
echo $result['postDate'];
i do get that what is stored(eg. 2011-03-16 16:48:24)
on the other hand when i print it through date function:
echo date('F/j/Y',$result['postDate'])
i get December/31/1969
what am i doing wrong?
many thanks
try this.
date('F/j/Y',strtotime($result['postDate']));
as timestamp is required, not formatted date as second parameter.
or you can also try
SELECT UNIX_TIMESTAMP(postDate) as postDateInt from myTable
instead of SELECT postDate from myTable
and then have this in your code.
date('F/j/Y',$result['postDateInt']);
The PHP date function looks for an int time() as the 2nd param. Try using strtotime()
echo date('F/j/Y', strtotime($result['postDate']) );
Why not format the date as needed in your MySQL query?
SELECT DATE_FORMAT(postDate, '%M/%D/%Y') as date from table
The PHP `date()' function expects a number for the second parameter - ie a unix timestamp.
You can convert a SQL date string (or virtually any other date string) into a timestamp in PHP by using the strtotime() function. At least two other answers have already suggested this.
However, I would suggest that you'd be better off getting the date out of your database in unix timestamp format in the first place. You can do this by querying using the MySQL UNIX_TIMESTAMP() function, as follows:
SELECT UNIX_TIMESTAMP(mydatefield) AS mydatefield_timestamp FROM mytable
..obviously, replacing the field and table names as appropriate.
Then you will get the date in timestamp format in your returned dataset in PHP, which you can pass directly into the date() function as follows:
echo date('F/j/Y',$result['mydatefield_timestamp']);
Hope that helps.

Change display format of date and time field in MySQL PHP

My select statement in PHP is
"select * from table";
I use the following PHP statement to display date & time of MySQL field.
<?php
echo $row['mdate'];
?>
The result come like this
2010-03-09 16:59:18
I want to view the result in the following format
09-03-2010 16:59:18
and I want to view the result in the following format
09-03-2010 4:59:18 PM
without defining any extra function. I can only modify my echo statement.
<?php echo $row['msgdate']; ?>
or
I can also modify my select statement.
See date_format():
select *, date_format(mdate, '%d-%m-%Y %H:%i:%s') AS formated_date from tabl;
And use formated_date in jouw php-code.
You can do the formatting directly in the database as Frank Heikens shows in his answer, or you can do it in PHP. Convert the mySQL date value to a UNIX timestamp using
$timestamp = strtotime($row["mdate"]);
then you can use all options of date() to format it, for example:
echo date("Y-m-d H:i:s", $timestamp); // returns 09-03-2010 16:59:18
both approaches are equally valid; I personally like to modify the value in PHP.

What is the simplest way to format a timestamp from SQL in PHP?

What is the simplest, fastest way to complete the PHP code below such that the output is in a user-friendly format (for example, "October 27, 2006")?
$result = mysql_query("SELECT my_timestamp FROM some_table WHERE id=42", $DB_CONN);
$row = mysql_fetch_array($result);
$formatted_date = ???($row['my_timestamp']);
echo $formatted_date;
You could use MySQL to do this for you,
$result = mysql_query("SELECT DATE_FORMAT(my_timestamp, '%M %d, %Y) AS my_timestamp FROM some_table WHERE id=42", $DB_CONN);
$row = mysql_fetch_array($result);
$formatted_date = $row['my_timestamp'];
echo $formatted_date;
Or use PHP,
$result = mysql_query("SELECT my_timestamp FROM some_table WHERE id=42", $DB_CONN);
$row = mysql_fetch_array($result);
$formatted_date = strftime('%B %d, %y', $row['my_timestamp']);
echo $formatted_date;
I tend to do the date formatting in SQL, like Aron's answer. Although for PHP dates, I prefer using the DateTime object (PHP5+) over date:
$timestamp = new DateTime($row['my_timestamp']);
echo $timestamp->format('F j, Y') . '<br />';
echo $timestamp->format('F j, Y g:ia');
You want the date() function.
If you've got a DATE or DATETIME column, you can use SELECT UNIX_TIMESTAMP(mycolumn) AS mycolumn to convert it to a unix timestamp for the date function to use.
You should definitely use the DateTime class (or any home grew equivalent class), over de Date function, and not use timestamps:
Timestamp don't work well in all environments for dates before 1970 - if you deal with birthdays, you're relying on code that may break on some servers
Be also very carefull of the use of strftime, it looks like a nice function, but it is very unrelyable, as it depends on setlocale, which is process-wide. What this means is that if your server is a windows box, you have one process per processor, and then the rest is multi-threaded - in other words, one setlocale in one script will affect the other scripts on the same processor - very nasty !
At the end of the day, don't rely on timestamps, unless you are in an english only environment, and deal with dates between 1970 and 2032 only...!
If you have your tables indexed on that date field and you use a mysql data format function in your call.. (ie .. SELECT DATE_FORMAT(my_timestamp, '%M %d, %Y) AS my_time ) it will destroy your indexing. Something to keep in mind. We have seen dramatic increases in speed in removing all functioning from our sql statements and letting php handle it all.
Functioning such as formatting dates and simple math
I use:
date("F j, Y", strtotime($row['my_timestamp']))
or you can change the SELECT to: DATE_FORMAT(field,'%d %M, %Y') as datetime
You have a choice. You can use the date() function in PHP and process the output from MySQL, or you can use the date_format() function in MySQL and have it return a formatted string to begin with.
In my experience is that it doesn't really matter which you use, but BE CONSISTENT. They have different formatting parameters, so if you use both in a given application you'll spend a lot of time trying to remember if 'W' gives you the name of the day of the week or the week of the year.
Have the database do the formatting for you. It is far less susceptible to error because you are not reading a string and converting it. Instead, the database is going from its native format to a string.

Categories