Display mysql datetime in human readable format with php? - php

Am saving to a mysql table using:
$query = mysql_query("INSERT INTO test SET postit='$postit',postdate=NOW()");
I'm then trying to display it using:
echo "<li>" . date("D, d M y H:i:s O",$row['timestamp']) . " - " . $row['postit'] . "</li>";
It's saving the correct time in the database, however it renders:
Thu, 01 Jan 70 01:00:00 +0100
Anyone point out the stupidity?

The PHP date() function uses a Unix timestamp as the second variable in the function. What you are passing to the function is a MySQL time stamp. Try using:
echo date("D, d M y H:i:s O",strtotime($row['timestamp']));

I always like to use this function for that:
function parse_sql_timestamp($timestamp, $format = 'd-m-Y')
{
$date = new DateTime($timestamp);
return $date->format($format);
}
This way we can even go beyond 2038 ;)

Related

How to make Php Timestamp Function For My zone

i am trying to make php timestamp function i make it like this
My code example
$t=time();
echo($t . "<br>");
echo(date("Y-m-d",$t));
but i want php timestamp in this format
Saturday, August 18, 2018 10:55:34 AM GMT+05:30
output:-1534569934
what i need to changes in my code
Here is what i tried,
<?php
$t = time();
echo(date("l, M d, Y h:i:s A",$t).' GMT '.date("O",$t));
I hope this solves your problem :-)
According to :
i want php timestamp in this format
Saturday, August 18, 2018 10:55:34 AM GMT+05:30
you can format your timestamp as this:
date('l ,F d, Y h:i:s A \G\M\T P');
you also said :
i am trying to make php timestamp function
First ,i don't see any function declaration nor return statement so you didn't try to make a function.However according to the format above and the fact that you want a custom timestamp function, you can build it this way:
function custom_timestamp(){
return date('l,F d,Y h:i:s A \G\M\T P');
}
Then you can use it anywhere as timestamp and based on your own logic.
You can try this:
<?php
$timestamp=time();
echo(date("F d, Y h:i:s A", $timestamp));
?>

How to make time in specific format from current timestamp in php?

I have a time stamp like 2014-05-26 16:39:32 . From this time stamp I want to make something 4:39 PM , 26thMay 2014 . Is there any built in function to this ? Or how can I do it ?
try
echo date('h:i A, dS M Y', strtotime('2014-05-26 16:39:32')); // 04:39 PM, 26th May 2014
without leading zero in time try 4:39
echo date('g:i A,dS M Y', strtotime('2014-05-26 16:39:32')); // 4:39 PM, 26th May 2014
For more read manual:- http://php.net/manual/en/function.date.php
echo date('g:i A,dS M Y',strtotime('2014-05-26 16:39:32'));
For more you can look into date function in php
Use DateTime class like this
$a = new DateTime("2014-05-26 16:39:32");
echo $a->format("h:i A, dS M Y");

PHP strtotime() function off using "1-January-2013"

I am collecting a user's birthday from a registration form and am hoping to convert this to a timestamp using strtotime before storing it in a database, but this isn't going so well.
I have them select their date in 3 select boxes, one is for the day, another the month, and lastly the year, and then I feed those into strtotime. The problem is, I am inputting "1", "January", and "2013" for the variables previously listed and am feeding them into strtotime like this:
$user->birthday = strtotime($input['bday'].'-'.$input['bmonth'].'-'.$input['byear']);
This reads when echoed as "1-January-2013", yet the timestamp it spits out renders to "Dec 31, 2012" using:
date_default_timezone_set("America/Los_Angeles");
echo date("M d, Y", 1356998400);
I have been testing different methods profusely but cannot get this to work. I'd appreciate any assistance.
Are you setting date_default_timezone_set() before the call to strtotime() or after? Since the default timezone is UTC, and LA is UTC-08.00, that could account for the difference.
You have to call date_default_timezone_set before calling strtotime, so that you have the same timezones when parsing the input date and when you're displaying the date.
Not too sure how your code is failing, but this works for me:
// Set the test data.
$test_data = '1-January-2013';
// Get the Unix datetime from the test data.
$date = strtotime($test_data);
// Format the Unix datetime.
$dateFormat = date("M d, Y", $date);
// Output for debugging.
echo 'test_data: ' . $date . '<br />';
echo 'date: ' . $date . '<br />';
echo 'dateFormat: ' . $dateFormat . '<br />';
Output from my side using PHP 5.5 is:
test_data: 1357016400
date: 1357016400
dateFormat: Jan 01, 2013
But on the outside chance those - dashes are choking things, try this:
// Set the test data.
$test_data = '1-January-2013';
// Filter out dashes from the '$test_data'
$test_data = preg_replace('/-/', ' ', $test_data);
// Get the Unix datetime from the test data.
$date = strtotime($test_data);
// Format the Unix datetime.
$dateFormat = date("M d, Y", $date);
// Output for debugging.
echo 'test_data: ' . $date . '<br />';
echo 'date: ' . $date . '<br />';
echo 'dateFormat: ' . $dateFormat . '<br />';
And again, the output from my side using PHP 5.5 is:
test_data: 1357016400
date: 1357016400
dateFormat: Jan 01, 2013
It's definitely a time zone issue. 1356998400 is 01 / 01 / 13 # 12:00:00am UTC, so when you do this:
date_default_timezone_set("America/Los_Angeles");
echo date("M d, Y", 1356998400);
The Unix time 1356998400 is Dec 31, 2012 in Los Angeles! I suspect you're getting your timestamp in UTC and then outputting it with PST.

Convert string to MySQL date

I need to be able to convert a string (see example below) to a format that can be added to MySQL. I could just add it as a var car but I need to be able to run queries based on these date ranges.
e.g. '02 May 2013 12:08 AM GMT'
Any suggestion of the best way to achieve this?
EDIT
Currently trying the following:
$date = date_create_from_format('d M Y H:i A e', '02 May 2013 12:08 AM GMT');
echo $date;
but getting a server error when I trying echoing the date.
I've also tried the following to break the date and timezone apart and try to deal with it separately:
$myvalue = '02 May 2013 12:08 AM GMT';
$arr = explode(' ',trim($myvalue));
$timezone = new DateTimeZone($arr[5]);
$arr[count($arr)-1]='';
$time=implode(' ',$arr);
$date = date_create_from_format('d M Y h:i A', $time, $timezone);
echo $date;
but again I'm getting a server error.
EDIT
I just realized that the second chunk of code might be going wrong due to the GMT as it doesn't appear to be a usable format with this function.
EDIT
After further investigation I think the best way to store the data is to have all dates stored as a DATETIMEin MySQL with the same timezone (gmt) and along with it storing the actual timezone they are and using the timezone when running queries if needed.
$myvalue = '02 May 2013 12:08 AM GMT';
$arr = explode(' ',trim($myvalue));
$timezone = new DateTimeZone($arr[5]);
$arr[count($arr)-1]='';
$time=implode(' ',$arr);
$timestamp = strtotime($time);
$date = date("Y-m-d H:i:s", $timestamp);
echo $date;
Why don't you just do this, then?
function convertToMysqlTime($string){
$seconds = strtotime($string);
if (!$seconds) return false;
return date("Y-m-d H:i:s", $seconds);
}
echo convertToMysqlTime('02 May 2013 12:08 AM GMT');
It accounts for that GMT part, too.
I hope you want to convert date obtained from javascript "Date()" function.
consider this function in javascript part:
function ISODateString(d) // to get date in format that MySQL accepts
{
function pad(n){return n<10 ? '0'+n : n}
return d.getUTCFullYear()+'-'
+ pad(d.getUTCMonth()+1)+'-'
+ pad(d.getUTCDate()) +' '
+ pad(d.getUTCHours())+':'
+ pad(d.getUTCMinutes())+':'
+ pad(d.getUTCSeconds())
}
after this use:
var d=ISODateString(new Date())
now to send it either use post method or GET with url encoding to utf-8
$date = date_create_from_format('d M Y H:i A e', '02 May 2013 12:08 AM GMT');
echo $date;
This code will always produce error messages, for different reasons.
If the PHP version is < 5.3 then the code will fail, because date_create_from_format() does not exist.
If the PHP version is >= 5.3 then the second line will fail, because $date is an instance of DateTime and that class does not have a __toString() method. Use DateTime::format() instead.
Use the DateTime class createFromFormat function. If your PHP version <= 5.3. Here's the documentation: http://www.php.net/manual/en/datetime.createfromformat.php
Edit:
$date = DateTime::createFromFormat('d M Y H:i A e','02 May 2013 12:08 AM GMT');
echo $date->getTimeZone()->getName(); //return 'UTC'
echo $date->format('Y-m-d H:i:s'); // return 2013-05-02 00:08
Set the session timezone in mysql:
You can use strtotime() to convert a textual representation of a time (such as your example) to a Unix timestamp:
$timestamp = strtotime($string);
Then you can store this value in MySQL's TIMESTAMP field type.

php filemtime 24 hr format

I created this code to get the date a file was last touched then display it to the user in AM/PM format.
It doesn't seem to be working though. I know I'm close; what am I doing wrong?
$filename = 'test.html';
if (file_exists($filename)) {
$date = date(filemtime($filename));
clearstatcache();
}
echo "- last updated: " . date('F d Y h:i A', strtotime($date));
Output: last updated: December 31 1969 06:59 PM
Try this:
if (file_exists($filename)) {
$date = filemtime($filename);
clearstatcache();
}
echo "- last updated: " . date('F d Y h:i A', $date);
In your code, this line:
$date = date(filemtime($filename));
wouldn't work since filemtime returns a UNIX timestamp, which you are then passing as the first parameter to date(). Even if that did work, you are then converting that date back to a UNIX timestamp with strtotime(), and then back into a date string again which seems a little inefficient.
Also consider what happens if the file doesn't exist, will $date have been set elsewhere in your code?
$date = date(filemtime($filename));
That line is wrong. First argument to date() is a format string. Replace with:
$date = filemtime($filename);
Also, you don't need to perform strtotime() on a timestamp, just use as is:
echo date('F d Y h:i A', $date);

Categories