Php strtotime not showing the valid date - php

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'.

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

PHP - strtotime() and datetime strange behavior

I have a some dates formatted as string in this format: 18-04-17.
I want to convert them to dates.
At first I used strtotime() to change it to date:
$timestamp = strtotime($row['reqEndDate']);
$newdate = date("d-m-Y", $timestamp);
echo $newdate;
This outputs: 17-04-2018. As you can see it mistakes the day with the year.
Next I tried to use datetime, as follows:
$newdate = datetime::createFromFormat("d-m-Y", $row['reqEndDate']);
echo $newdate->format('d-m-Y');
This outputs: 18-04-0017 .This method gets the day correctly, but instead of 2017 it prints 0017.
Is there any reason for this behavior? Maybe some settings in my php setup to look for?
Uppercase Y will produce 4 digit year. Lowercase y produces a two-digit year. Your code with DateTime should be:
$date = DateTime::createFromFormat('d-m-y', $row['reqEndDate']);
echo $date->format('d-m-Y');
You can read about supported formats in PHP's manual page about 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.

Today's date format - Day (Shorthand) Date Number Month (Shorthand) - PHP

I have a feed which gives feed in the following format: "Fri 14 Oct"
I want to see if today's date matches the date from the feed. My problem is the format of today's date/
$today = date("d m");
This outputs 17 10.
What is the best way to format $today so that it outputs Day (shorthand) space date (number) Month (shorthand) ?
how about:
$today = date("D j M");
As explained in date() reference manual.
Anyway you should be aware of timezone issues unless you are 100% sure that your server is in the same timezone of the feed you are comparing.
I would follow a different approach though, you can parse the feed's date using DateTime::createFromFormat() which also understand timezones, and then compare it with today's date.
$today = date("D d M");
PHP Date Documentation
<?php
// Prints the day
echo date("l") . "<br>";
// Prints the day, date, month, year, time, AM or PM
echo date("l jS \of F Y h:i:s A");
?>
For more details, please visit http://www.w3schools.com/php/func_date_date.asp

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