Simple PHP date question - php

I have the following string:
$date = 2011-08-29 14:53:15;
when I do this:
echo date('F j, Y', $date);
I get December 31, 1969 instead of August 29, 2011. How to get the right date?

Use strtotime on your $date value.

echo date('f j, Y',strtotime($date));

<?php
$date = strtotime($date);
echo date('F j, Y', $date);
?>
To explain...
The function date() is expecting a unix time-stamp (something like 23498034). the function strtotime() takes a normal looking date that a person would make, and converts it to a timestamp. Then you're good to go.

Related

php change the string into time format

In php I have time like this
$time = '2015-06-29T16:00:00Z';
I want to convert that time like this format Tuesday, December 16, 2015 3:00 PM
For that I tried
echo date( 'jS F Y', strtotime( $time) );
but it is showing time like 1st January 1970
So can someone help me to get the actual time format as I want.
A simple DateTime class usage should suffice, just feed it into the constructor, the just use ->format and provide the desired output format:
$time = '2015-06-29T16:00:00Z';
$date = new DateTime($time);
echo $date->format('jS F Y');
Sample Output
You can use the DateTime class for better handling of dates
$time = '2015-06-29T16:00:00Z';
$dateTime = new DateTime($time);
echo $dateTime->format('l, F d, Y g:i A');
$time = '2015-06-29T16:00:00Z';
echo date( 'l, F j, Y H:i A',strtotime($time));
l, F j, Y H:i A can be re-ordered to change the output.
About date function, http://php.net/manual/en/function.date.php
Just pass proper format parameters to it.
$time = '2015-06-29T16:00:00Z';
echo date( 'l, F j, Y g:i A', strtotime( $time) );
Use preg_split:
$parts = preg_plit("/Z/",$time);
$parts = preg_split("/T/",$parts[0]);
$theDate=$parts[0];
$theTime=$parts[1];
$what_you_want=date(strtotime($theDate." ".$theTime);
Note that you can still change the format of the output.

Errors with using a timestamp in PHP

I am receiving the error: A non well formed numeric value encountered
Here is my code:
<?php
$timestamp= '2013-01-20 18:20:20';
$datetime= date('F j, Y', $timestamp);
echo $datetime;
?>
This returns January 1, 1970 Which isn't right. What am I doing wrong?
BTW: All of my $timestamp variables will be in that format. I am using datetime in my MySQL database table.
Thanks
the date function takes a timestamp which is an int
you need to call it using
date('F j, Y', time());
or
$timestamp= '2013-01-20 18:20:20';
date('F j, Y', strtotime($timestamp));
see http://php.net/manual/en/function.date.php for info on how to use the date function
'2013-01-20 18:20:20' is not a timestamp . You have to convert it to timestamp. You can use strtotime function to do this.
$timestamp= strtotime('2013-01-20 18:20:20');

Convert date integer to text?

Is there anyway to convert the value of idate("z") to a date format that reads the Day, Month, and Year? My code looks like this:
$date_int = idate("z");
$date_text = strtotime($date_int);
$date = date("l, F j, Y", $date_text);
For some reason, it's still echoing Thursday, January 1, 1970.
Any ideas?
idate("z") is incorrect as that will return the day of the year. It seems like you want idate("U"), but in that case just use date() without the second parameter, it will assume time(). Example:
$date = date("l, F j, Y");
That should be all you need.
idate("z") will only return the day of the year. Today being 74. strtotime will not understand 74 as a parsing format. Therefore date() fails all together.
Assuming you want the date of the CURRENT YEAR:
$dayOfYear = 80;
print date("l, F j, Y", strtotime($dayOfYear - idate("z") . " day")); // Wednesday, March 21, 2012
$dayOfYear = 1;
print date("l, F j, Y", strtotime($dayOfYear - idate("z") . " day")) // Monday, January 2, 2012

How to format date yy-mm-dd in PHP

I have dates currently in this format: yy-mm-dd (e.g. 2011-11-18)
I want them in this format: Friday 18 November 2011
I've tried reading through the PHP documentation manual, but I can't see how to manage dates in the format that I have. If the date needs to be in a different order I can arrange that, but I'm a bit stuck at the meoment.
Any help would be much appreciated.
Use PHP5s new date classes. Much cleaner:
$date = DateTime::createFromFormat('Y-m-d', '2011-11-18');
echo $date->format('l d F Y');
date('l j F Y', strtotime($date));
Just use starttime to change the the dates in many formats using this link.
echo date('l d F Y');
gives you the date format you want.
This was all in the manual you yourself linked.
just use strtotime to get back a timestamp and then use date() to format that:
$date = '2011-11-18'; // your date
$timestamp = strtotime($date); // convert to a timestamp
$new_date = date('l j F Y',$timestamp) // format timestamp
echo $new_date;

How to get AM/PM from a datetime in PHP [duplicate]

This question already has answers here:
Convert from MySQL datetime to another format with PHP
(18 answers)
Closed 2 years ago.
I have a date time in a variable. My format is 08/04/2010 22:15:00. I want to display this like 10.15 PM. How to do this in PHP?
You need to convert it to a UNIX timestamp (using strtotime) and then back into the format you require using the date function.
For example:
$currentDateTime = '08/04/2010 22:15:00';
$newDateTime = date('h:i A', strtotime($currentDateTime));
$dateString = '08/04/2010 22:15:00';
$dateObject = new DateTime($dateString);
echo $dateObject->format('h:i A');
Use strtotime() to make the date a UNIX timestamp.
For output, check out the various options of date().
$timestamp = strtotime("08/04/2010 22:15:00");
date("h.i A", $timestamp);
<?php
$dateTime = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
echo $dateTime->format("d/m/y H:i A");
?>
You can use this to display the date like this
22/06/15 10:46 AM
Like this:
$date = '08/04/2010 22:15:00';
echo date('h:i A', strtotime($date));
Result:
10:15 PM
More Info:
date
strtotime
for flexibility with different formats, use:
$dt = DateTime::createFromFormat('m/d/Y H:i:s', '08/04/2010 22:15:00');
echo $dt->format('g:i A')
Check the php manual for additional format options.
PHP Code:
date_default_timezone_set('Asia/Kolkata');
$currentDateTime=date('m/d/Y H:i:s');
$newDateTime = date('h:i A', strtotime($currentDateTime));
echo $newDateTime;
Output: 05:03 PM
$currentDateTime = $row['date'];
echo $newDateTime = date('l jS \of F Y h:i:s A', strtotime($currentDateTime));
Perfect answer for AM/PM live time solution
<?php echo date('h:i A', time())?>
Just simply right A
{{ date('h:i A', strtotime($varname->created_at))}}
For (PHP >= 5.2.0):
You can use DateTime class. However you might need to change your date format. Didn't try yours.
The following date format will work for sure: YYYY-MM-DD HH-MM-SS
$date = new DateTime("2010-04-08 22:15:00");
echo $date->format("g"). '.' .$date->format("i"). ' ' .$date->format("A");
//output
//10.15 PM
However, in my opinion, using . as a separator for 10.15 is not recommended because your users might be confused either this is a decimal number or time format. The most common way is to use 10:15 PM
It is quite easy. Assuming you have a field(dateposted) with the type "timestamp" in your database table already queried and you want to display it, have it formated and also have the AM/PM, all you need do is shown below.
<?php
echo date("F j, Y h:m:s A" ,strtotime($row_rshearing['dateposted']));
?>
Note: Your OUTPUT should look some what like this depending on the date posted
May 21, 2014 03:05:27 PM

Categories