Date Format in PHP - php

I am having a problem with formatting a date that I receive from a MySQL database. I keep getting this error:
Notice: A non well formed numeric value encountered in C:\xampp\htdocs\index.php on line 23
Here is my code:
$news_query = $database->query("SELECT * FROM " . DB_NEWS . " ORDER BY 'date'");
while($news_data = mysql_fetch_array($news_query,MYSQL_ASSOC)){
$date = date("M d, Y - g:i:s A", $news_data['date']);
echo "
<DIV class = 'news_post'>
<DIV class = 'news_title'>" . $news_data['title'] . "</DIV>
<DIV class = 'news_info'>Posted on " . $date . " By " . $news_data["author"] . "</DIV>
<DIV class = 'entry'>" . $news_data['entry'] . "</DIV>
</DIV>";
}
I have read over the PHP manual for timestamps and the date function and I can't seem to figure out what my problem is.

Are you using datetime format in MySQL?
Try this:
<?php
date("M d, Y - g:i:s A", strtotime( $news_data['date'] ) );
?>

The date from the database in $news_data['date'] is probably a string representation of a date. The date() function takes a format string and an optional timestamp, which is an integer. This is the reason for the error you get.

Most likely, strtotime() will convert your date into the format required for date().
Instead of:
$date = date("M d, Y - g:i:s A", $news_data['date'])
You want:
$date = date("M d, Y - g:i:s A", strtotime($news_data['date']))

Try this query
$database->query("SELECT *,unix_timestamp(date) as date
FROM ".DB_NEWS." ORDER BY date")

There are two possibilities.
The first is : use UNIXTIMESTAMP sql function in your query, or strtotime like said before.
The second, which I prefer is using DateTime PHP object.
$date = new DateTime($datas['date']);
//if an error occurs because of the format use createFromFormat
$date = DateTime::createFromFormat('d-m-y',$datas['date']);//for exemple for dd-mm-YYYY format

Related

How to get the following date '1333504225' in to '0000-00-00' with php?

For some reason this is how I get the date. I have tried using php to decode it but I get the wrong date.
$datetst ='1333504225';
$date = strtotime($datetst);
echo date("F j, Y", $date);
What I plan to do with this is calculate the difference in days from (today-the day the record was created). Which is why I need to have it in '0000-00-00'. I am no expert, but our engineer is of no help.
No need to convert your unix timestamp again by strtotime
just use this
$datetst =1333504225;
echo date("F j, Y", $datetst);
and to get difference in days use below code
$datetst = 1333504225;
echo "Date : ".date("F j, Y", $datetst);
$diff = time() - 1333504225 ;
$diffDay = floor($diff/(3600*24));
echo "\nDays from now : ".$diffDay;
Demo link : https://eval.in/716084

Regex to convert already formatted date to different format

I have $date = $run['at'];, which gives me 2013-06-03T16:52:24Z. How can I manipolate it to get for example "d M Y, H:i" ?
You should use the DateTime class.
$date = new DateTime($run['at']);
echo $date->format('d M Y, H:i');
You can use:
$time = strtotime($run['at']);
echo date("Y-m-d", $time);
But, where does $run['at'] come from?

Format string to date

I am having difficulty formatting a string to a human-readable date format.
I have:
07122012
and need to get
7 Dec 2012
Tried:
date("j M Y", strtotime($str));
You can use DateTime::createFromFormat (available with PHP >= 5.3.0) like this:
$date = DateTime::createFromFormat('jmY', '07122012');
echo $date->format('d M Y') . "\n";
try:
$str = "07122012";
$stryear = substr($str,-4);
$strmonth = substr($str,2,2);
$strday = substr($str,0,2);
echo date("j M Y", strtotime($stryear.'-'.$strmonth.'-'.$strday));
it will do the trick, but the best thing is to read the php documentation (probably there is a built in feature)
I think the most elegant way, since you cannot parse this string directly, is this:
$date = "07122012";
// parse the dte fist correctly
$parsed_date = date_parse_from_format('dmY', $date);
// Make time, print date
echo date("j M Y", mktime(0, 0, 0, $parsed_date['month'], $parsed_date['day'], $parsed_date['year']));

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);

Convert DateTime to String PHP

I have already researched a lot of site on how can I convert PHP DateTime object to String. I always see "String to DateTime" and not "DateTime to String"
PHP DateTime can be echoed, but what i want to process my DateTime with PHP string functions.
My question is how can I make PHP dateTime Object to a string starting from this kind of code:
$dts = new DateTime(); //this returns the current date time
echo strlen($dts);
You can use the format method of the DateTime class:
$date = new DateTime('2000-01-01');
$result = $date->format('Y-m-d H:i:s');
If format fails for some reason, it will return FALSE. In some applications, it might make sense to handle the failing case:
if ($result) {
echo $result;
} else { // format failed
echo "Unknown Time";
}
echo date_format($date,"Y/m/d H:i:s");
The simplest way I found is:
$date = new DateTime(); //this returns the current date time
$result = $date->format('Y-m-d-H-i-s');
echo $result . "<br>";
$krr = explode('-', $result);
$result = implode("", $krr);
echo $result;
I hope it helps.
There are some predefined formats in date_d.php to use with format like:
define ('DATE_ATOM', "Y-m-d\TH:i:sP");
define ('DATE_COOKIE', "l, d-M-y H:i:s T");
define ('DATE_ISO8601', "Y-m-d\TH:i:sO");
define ('DATE_RFC822', "D, d M y H:i:s O");
define ('DATE_RFC850', "l, d-M-y H:i:s T");
define ('DATE_RFC1036', "D, d M y H:i:s O");
define ('DATE_RFC1123', "D, d M Y H:i:s O");
define ('DATE_RFC2822', "D, d M Y H:i:s O");
define ('DATE_RFC3339', "Y-m-d\TH:i:sP");
define ('DATE_RSS', "D, d M Y H:i:s O");
define ('DATE_W3C', "Y-m-d\TH:i:sP");
Use like this:
$date = new \DateTime();
$string = $date->format(DATE_RFC2822);
Shorter way using list. And you can do what you want with each date component.
list($day,$month,$year,$hour,$min,$sec) = explode("/",date('d/m/Y/h/i/s'));
echo $month.'/'.$day.'/'.$year.' '.$hour.':'.$min.':'.$sec;
Its worked for me
$start_time = date_create_from_format('Y-m-d H:i:s', $start_time);
$current_date = new DateTime();
$diff = $start_time->diff($current_date);
$aa = (string)$diff->format('%R%a');
echo gettype($aa);

Categories