strtotime() not working for some reason - php

I have a string which is in the format: dd/mm/yy
e.g. 29/03/14
But when I print it using date() I get a completely different date!
What am I missing?
$endDate = "29/03/14";
echo date("jS F, Y", strtotime( $endDate ));
1st January, 1970
I even tried:
echo date("jS F, Y", strtotime( trim($endDate) ));
With no luck!
I am reading $endDate from a text file
What I am trying to do is find out if it is the last day of the month...
i.e. Current month is 03 - March has 31 days
The day of month in file is 29
This is not the last day of the month

Your date format is not valid.
Check the Date Formats
You might consider using date_create_from_format('d/m/y','29/03/14'); and work with the DateTime object.

Use DateTime::createFromFormat
$date = DateTime::createFromFormat('d/m/y', '29/03/14');
echo $date->format("jS F, Y");
For getting the last day
echo $date->format('t');

Your date format isn't recognized by strtotime(). Use DateTime class instead:
$dateObj = DateTime::createFromFormat('d/m/y', $str);
echo $dateObj->format('jS F, Y');
Output:
29th March, 2014
Demo
What I am trying to do is find out if it is the last day of the month...
For that, you just have to check if the given date is the same as the last day of the month (which can be obtained with t format character)
$str = '29/03/14';
$dateObj = DateTime::createFromFormat('d/m/y', $str);
if ($dateObj->format('d') == $dateObj->format('t')) {
echo 'Given date is the last day of the month';
}

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

How do I echo out a day in the future using date()?

I want to always echo out the date 3 days from now. So right now I have:
$date = date("l F jS");
echo $date;
Which echos "Friday June 5th"
What exactly do I do so that it echos out "Monday June 8th" and tomorrow it echos out "Tuesday June 9th" (always 3 days ahead).
You can use strtotime() with a relative date format. When you pass a Unix timestamp as the second parameter to date() it will format that date.
$date = date("l F jS", strtotime('+3 days'));
echo $date;
Or if you prefer OOP use DateTime(). With DateTime() you can put the relative date format right into its constructor. It also handles things like daylight savings time which may come into play depending on what you're doing.
$date = new DateTime('+3 days');
echo $date->format("l F jS");

How can I get the next friday of a given day?

I have a problem in PHP:
How can I get the next friday of a given day?
E.g.
What's the date of next friday following monday 6th April 2015?
Is there a way to pass as a parameter the wanted day to strtotime( "next friday")?
Ok, got it! Thanks to all!
The problem with my dates is that they formated like d/m/Y, and I was messing it all up.
$dt = explode("/", $_SESSION['conj']['dtEnd'][0]);
$newDate = $dt[2] ."-".$dt[1]."-".$dt[0];
$nextFriday = date ('d/m/Y', strtotime("next friday", strtotime($newDate)));
<?php
$time = strtotime('Monday, April 6, 2015');
$next = strtotime('next friday, 11:59am', $time);
echo date('l, F j', $next);
?>

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

PHP get 31 days distance from starting date

How can I get what date it will be after 31 days starting with $startDate, where $startDate is a string of this format: YYYYMMDD.
Thank you.
strtotime will give you a Unix timestamp:
$date = '20101007';
$newDate = strtotime($date.' + 31 days');
you can then use date to format that into the same format, if that's what you need:
echo date('Ymd', $newDate);
If you're using PHP 5.3:
$date = new DateTime('20101007');
$date->add(new DateInterval('P31D'));
echo $date->format('Y-m-d');
The pre-5.3 date functions are lacking, to say the least. The DateTime stuff makes it much easier to deal with dates. http://us3.php.net/manual/en/book.datetime.php
Just a note that +1 month will also work if you want the same date on the next month and not 31 days exactly each time.
echo date('Y m d',strtotime('+31 Days'));

Categories