DateTime class doing wrong conversion of Timestamp? - php

The following code is producing a wrong conversion of a Timestamp (1350553368):
$dateTime = new DateTime();
$dateTime->setTimeStamp(1350553368);
echo $dateTime->format('F n, Y');
PHP converts it to October 10, 2012: http://codepad.viper-7.com/clum0f
However, that timestamp is actually for October 18, 2012: http://www.onlineconversion.com/unix_time.htm
I'm sure it's me, and not PHP, so what am I doing wrong? The code is pretty straightforward, so I can't figure it out.

You are using format 'F n, Y'. n is a numeric representation of the month (October is month 10). Use d (leading zeroes) or j (no leading zeroes). See PHP date() reference.
echo $dateTime->format('F d, Y');

Form PHP DOC
n = Numeric representation of a month, without leading zeros
d = Day of the month, 2 digits with leading zeros
You should replace
$dateTime->format('F n, Y');
With
$dateTime->format('F d, Y');

Related

How to use date() and strtotime() to get a date from a string?

I am taking input following Datetime : 12.01.2017 2:25 pm
But while I passed above datetime on php function strtotime then it's doesn't returning exact datetime as 24 hours format.
Here is my scripts:
$sdate="12.01.2017 2:25 pm";
date("d.m.Y H:m", strtotime($sdate))
It's returning always 12-jan-2017 14:01:00
But accurate output should be : 12-jan-2017 14:25:00
Please let me know how can get accurate datetime.
Thanks
hey there is problem on your dateformat string :
it should be H:i instead of H:m on your scripts.
Please correct date and format it accordingly your expectation:
date("d-M-Y H:i", strtotime($sdate));
<?php
$sdate="12.01.2017 2:25 pm";
echo date("d-M-Y H:i", strtotime($sdate));
?>
Output
You should use i instead of m in the date:
like this
echo $sdate="12.01.2017 2:25 pm";
echo date("d.m.Y H:i", strtotime($sdate));
There are already answers that use strtotime(). I would like to show you the DateTime class. I find it very usefull and better looking in general. Not that it matters to most people but still.
// Date string
$dateString = '12.01.2017 11:03 am';
// Create date object from string
$oDate = DateTime::createFromFormat('d.m.Y h:i a', $dateString);
// Output the date
echo $oDate->format('d.m.Y h:i a');
// Or some other format
echo $oDate->format('Y-m-d H:i:s');
Note that date() and DateTime accept the same date parameters. Your string suggests that you will need the following parameters.
d - Day of the month, 2 digits with leading zeros
m - Numeric representation of a month, with leading zeros
Y - A full numeric representation of a year, 4 digits
h - 12-hour format of an hour with leading zeros
i - Minutes with leading zeros
a - Lowercase Ante meridiem and Post meridiem
More information
For more information about date, DateTime, strtotime() and all parameters you can use:
date() - Manual
DateTime - Manual
strtotime() - Manual
date() and DateTime manuals list all possible parameters

Creating DateTime object with DateTime::createFromFormat() fails

I have been given a date string from an API in the format e.g.
20120522T143127
I am trying to convert this to a DateTime object, but the object creation fails because of the 'T' (I think).
This is my current code:
$date = DateTime::createFromFormat( "YMDTHis", '20120522T143127' );
$result = $date->format( $format );
What am I missing?
I also tried:
$date = DateTime::createFromFormat( "YMD\THis", '20120522T143127' );
Your format is a bit wrong:
M A short textual representation of a month, three letters Jan through Dec
Use m instead of M
m Numeric representation of a month, with leading zeros 01 through 12
D A textual representation of a day, three letters Mon through Sun
Use d instead of D
d Day of the month, 2 digits with leading zeros 01 to 31
Just escape the T with a backslash
So just use:
Ymd\THis
echo date('Y-m-d H:i:s',strtotime('20120522T143127'));

php not friendly with date format d/m/Y

I can't convert date to format d/m/Y.
date("d-m-Y", strtotime(substr($code, 22, 6)) ); return 21-10-2014
but date("d/m/Y", strtotime(substr($code, 22, 6)) ); return 21/10/2014
How to format?
I would use DateTime to create an object with your format, and then reformat it.
<?php
$date = DateTime::createFromFormat('ymd', '140521');
echo $date->format('d/m/y'); //Output: 21/05/14
https://eval.in/208227
How would you like your formated date be displayed? The d, m and y in "d-m-Y" or "d/m/Y" are responsible for what you wish to display, not - or /
Here is a few list of parameters and their output:
d : Day of the month, 2 digits with leading zeros 01-31
m : Numeric representation of a month, with leading zeros 01-12
Y : A full numeric representation of a year, 4 digits
F : A full textual representation of a month, such as January or March
l : A full textual representation of the day of the week Sunday through Saturday
If you want to display 21 October 2014, you will have to use:
date("d F Y", strtotime(substr($code, 22, 6)) );
$originalDate = "2014-10-21";
$newDate = date("d/m/Y", strtotime($originalDate));
(see strtotime and date docs on the PHP site).

how to convert date format in textual date format?

I have displayed a date in a php page which is in dd-mm-yyyy format like-
$bdate = new DateTime($_SESSION['booking_date']);
$date2=$bdate->format('d-m-Y');
echo $date2;
whic gives output as "23-04-2013".
but i want this like "23rd April 2013". How can i do this in php?
Check out date() formatting strings to understand how DateTime::format() works.
print $bdate->format('jS F Y');
Try this code
$bdate = new DateTime($_SESSION['booking_date']);
$date2=$bdate->format('l jS F Y');
echo $date2;
Do you consider in use Javascript ?
You can use the moment.js
[year, month, day, hour, minute, second]
moment([2010, 1, 14, 15, 25, 50]); // February 14th, 3:25:50 PM
For example... but you can find all in www.momentjs.com
this will work for you
$date2=$bdate->format('jS F Y');
echo $date2;
where
j - Day of the month without leading zeros
S - English ordinal suffix for the day of the month, 2 characters
F - A full textual representation of a month, such as January or March
Y - A full numeric representation of a year, 4 digits

String date current date/time?

I am using $date = date("D M d, Y G:i");.
When I echo $date, it shows the correct date/time. Now I need this as an string.
I have tried string($date); but nothing happens here. And
$today = strtotime($date);
here I get weird numbers..
I need a string so I can put $today in a message.
What is the correct method for this?
The date() function already returns a string.
Doing this :
$date = date("D M d, Y G:i");
You'll have the current date in the $date variable, as a string -- no need for any additional operation.
If you like working with objects you can do this:
$date = new \DateTime('now');
echo $date->format('D M d, Y G:i');
Your $date variable is a string, there's no need for any conversion.
You can have a look at the documentation: http://ch.php.net/manual/en/function.date.php. The return value of the date() function is string.
The strange numbers you see when you call strtotime() is the Unix timestamp which represents the number of seconds elapsed since January 1 1970 00:00:00 UTC.
You're already getting a string. $date can be used like any string now.
strtotime() actually gives you the number of seconds in time like unix
$date = 'Today is '.date("D M d, Y G:i", time());
echo $date;
With regards to:
$today = strtotime($date);
Those numbers are the current timestamp (the number of seconds since January 1st 1970).
You can use this as a second parameter in the date function to change the date to whatever you want.
$newDate = date("D M d, Y G:i", $timeStamp);

Categories