PHP datetime and strtotime got extra 3 minute - php

php date is get extra 3 minute after i use strtotime function. Please
<?php
$date=date('2014-03-03 09:00:00');
$date1=strtotime($date);
echo date('Y-m-d h:m:i',$date1);
?>
output: 2014-03-03 09:03:00

<?php
$date=date('2014-03-03 09:00:00');
$date1=strtotime($date);
echo date('Y-m-d h:i:s',$date1);
?>
you have used h:m:s , where m is month... :-)

Use the correct format of datetime.
Replace h:m:i with h:i:s

You are using m which is a numeric representation of a month (from 01 to 12)
That's why you are getting 03
change from
echo date('Y-m-d h:m:i',$date1);
to
echo date('Y-m-d h:i:s',$date1);

Related

date conversion in CodeIgniter / PHP does not gives output

I have date in this type of format: April 1st 2017 and I want to convert it into this type of format: 2017/04/01 in my CodeIgniter code using php. I have used below posted piece of code but it is not working. Please solve the issue.
Code:
$date = DateTime::createFromFormat('m/d/Y', "April 1st 2017");
echo "Date = ".$date->format('Y-m-d');
You can use strtotime() and date() php functions as
$newDate = date("m/d/Y", strtotime("April 1st 2017"));
Or in CodeIgniter
$date = DateTime::createFromFormat('j F Y - H:i', 'April 1st 2017');
echo $date->format('m/d/Y H:i:s');
Your format can be used in the constructor of DateTime. See accepted formats.
$date = new DateTime("April 1st 2017");
echo "Date = ".$date->format('Y-m-d');
Outputs:
Date = 2017-04-01
If you want to use DateTime::createFromFormat(), you have to use the proper format
"F jS Y"
The format you specified for your date is incorrect.
It would convert '04/01/2017' but it does not suit
April 1st 2017.
Try instead: createFromFormat('F dS Y')
Explanation:
F - full textual representation of a month, such as January.
d - day
S - English ordinal suffix for the day of the month
Y - 4-digit representation of year
you can try this also
<?php
$date='22 march 2018';
echo date('m/d/Y', strtotime($date));
?>

strtotime() ads a minute of month change

I am adding a month to a string fromated date with this code:
$str ="2017-01-29 14:22:57";
$effectiveDate = strtotime("+1 months", strtotime($str));
echo "<br>";
echo date('Y-m-d h:m:s',strtotime($str));
echo "<br>";
echo date('Y-m-d h:m:s',$effectiveDate);
The output is:
2017-01-29 02:01:57
2017-03-01 02:03:57
I am wondering, why is there a minute change? It seems that every month there is a 1 min change.
I'll just pop this in as a community wiki; I don't want rep for this, nor should there be any made from it.
From the manual http://php.net/manual/en/function.date.php
m => m Numeric representation of a month, with leading zeros 01 through 12
You want i for minutes. Instead of h:m:s do h:i:s that's why.
You're formatting as hour:month:seconds, to have the timestamp you'll want to do:
echo date('Y-m-d h:i:s', $effectiveDate);
See the date documentation for more information.

amazing result of date format in php

I am trying to change the date 13 December, 2016 using date('Y-m-d',strtotime('13 December, 2016')) and it gives me the result of 2017-12-13, what i am missing here
Actually that comma within the date strings create an issue while using the strtotime function you can replace it with str_replace or instead you can simply use DateTime::createFromFormat method so no need of using extra function like as
$date = DateTime::createFromFormat('d F, Y','13 December, 2016');
echo $date->format('Y-m-d');
or simply date_create_from_format
$date = date_create_from_format('d F, Y','13 December, 2016');
echo $date->format('Y-m-d');
date('Y-m-d', strtotime('13 December 2016'));
You need to remove your comma.
This will give the expected result.
Try this
$date = '13 December 2016';
echo date('d-m-Y', strtotime($date));
The m formats the month to its numerical representation there.
Mistake: You don't have to put coma there.
use
$time = strtotime('10/21/2016');
$newformat = date('Y-m-d',$time);
echo $newformat;

Php strtotime not showing the valid date

i have a mysql table with column type DATETIME, i want it to display like 19 Aug, 2013.
so i have tried with
echo $date = date('Y-m-d H:i:s');
echo '<br/>';
echo date('y M ,Y',strtotime($date));
The output im getting is
2013-08-19 22:47:12
13 Aug ,2013
The i tried with
$datetime = DateTime::createFromFormat('Y-m-d', '2013-08-19');
echo $datetime->format('yM,Y');
But it also outputs the wrong date 13Aug,2013
Any one have faced the same kind of issue.
y is two-digit year, you want d, which is day. See also the documentation.
You used y twice:
echo $date = date('Y-m-d H:i:s');
echo '<br/>';
echo date('d M ,Y',strtotime($date));
To me it looks like it's doing exactly what it should - but the format specifier you are passing to date() and dateTime->format() looks strange - 'y' returns the year as 2 digits, 'Y' returns a 4 digit year. Did you mean that you wanted the day of the month at the start of he output?
Try 'd' or 'j' in place of 'y'.

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