Convert date integer to text? - php

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

Related

How to Convert Date and Time String into Different format?

I am saving date and time in a single column i.e 2015-04-15 13:22:58 and I want to show this time and date in this format: 15th April 2015, 1:22 pm.
I have used this code:
$dt = '2015-04-15 13:22:58';
echo date("jS F Y", strtotime($dt));
and it shows only date (15th April 2015). How can I fetch time along with date?? Any help would be highly appreciable.
Try
$dt = '2015-04-15 13:22:58';
echo date("jS F Y h:i a", strtotime($dt));
//Result is 15th April 2015 01:22 pm
echo date("jS F Y g:i a", strtotime($dt));
// Result is 15th April 2015 1:22 pm
change "jS F Y" to "js F Y g:i a". That will output the time in your desired format as well as the date.
$dt = '2015-04-15 13:22:58';
echo date("jS F Y g:i a", strtotime($dt));
Output:
15th April 2015 1:22 pm
if you want to format date time in php, you can use date_format like this.
echo date_format(strtotime($dt), "jS F Y g:i A");

How to Display date as day/month/year

I want to display my date as day/month/year.
example 31st October 2011
echo '<h4>Date Of Birth: '.$row['dob'].'</br></h4>';
You can try below code ::-
echo '<h4>Date Of Birth: '.date('dS F Y', strtotime($row['dob'])).'</br></h4>';
// ^^^^^^^ Here the is format of date, in which format you want.
Output will be ::-
31st October 2011
For more you can refer click here
<?php
$mydate = "2010-03-3";
$newDate = date("d M Y", strtotime($mydate));
$new_date = date('dS F Y', strtotime($newDate));
echo $new_date;
?>
/*Out Put*/
03rd March 2010
So if you have that data stored as or you can convert it to 3 variables representing DD MM YYYY then I'd suggest creating array of months and an array of post-fixes(don't know how to call it, but in your example it's 31st) and getting DD % 10 to use reminder for post-fix array
$month = ['January','February',....,'December']; //remember it's $month[MM-1];
$post = ['th','st','nd',...,'th'] //not sure if this on is correct but hope you get the idea
echo date('dS F Y', strtotime($row['dob']));
Refer to DateTime. I normally avoid strtotime() conversions.
Heres a working solution:
<?php
$date = '2016-11-12';
$newDate = new DateTime($date);
echo $newDate->format('jS F Y');
?>
OUTPUT:
12th November 2016

PHP- strtotime not converting properly

I don't understand why strtotime is returning a date off by a few days.
$expiration = '2015-07-19'; // yyyy-mm-dd
$exp = strtotime($expiration);
$exp = date('F m, Y',$exp);
echo $exp; // returns "July 07, 2015" (NOT the 19th)
What am I missing?
UPDATE: Even if I do this:
echo date('F m, Y');
It says it's August 8 when today is August 21! Why!?!?!?
Here it is: Outputs July 19, 2015. See demo. You are using two characters to represent months in your date function. F and m are textual and numeric representations of a month. So change 'm' to 'd' to represent days. date("F d, Y") e.g. January 01, 2000
$expiration = '2015-07-19'; // yyyy-mm-dd
$exp = strtotime($expiration);
$exp = date('F d, Y',$exp);
echo $exp;
DEMO
Try this
$expiration = '2015-07-19'; // yyyy-mm-dd
$exp = strtotime($expiration);
//$exp = date('F m, Y',$exp);
$exp = date('jS F, Y',$exp);
Output
19th July, 2015

strtotime() not working for some reason

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';
}

Last weekday from specific date in php

I am looking for the best way to get the last weekday from a particular date. The example I am using is what was the last workday before Christmas eve (24th Dec).
unfortunately this doesn't work:
echo date('l jS \of F Y h:i:s A', strtotime('last weekday before 24th December 2012'));
Just remove before and your example will work fine (as of PHP 5.2.0). The absolute date part (24th December 2012) is processed first, followed by the relative part (last weekday), as per the relative formats documentation.
Original
last weekday before 24th December 2012
Correct
last weekday 24th December 2012
Per the other answers, previous and last when used as in the question behave in the exact same way; meaning the immediately preceding occurrence of something (in this case, a weekday). last does have another special meaning when used in the style of last dayname of, which is not being used in the question.
Reference:
Relative Formats manual page
And in fact each of the date/time formats pages document the available formats.
Have you tried something like this:
echo date('l jS \of F Y h:i:s A', strtotime('24th December 2012 previous weekday'));
This will output something like Friday 21st of December 2012 12:00:00 AM using PHP 5.3.19
Heres another way you could go about this, its not the prettiest thing but it should work:
$date = '24th December 2012';
$dateN = intval(date('N', strtotime($date)));
if ($dateN === 1) {
$prevWeekday = date('l jS \of F Y h:i:s A', strtotime($date . '-3 days'));
} else if ($dateN === 7) {
$prevWeekday = date('l jS \of F Y h:i:s A', strtotime($date . '-2 day'));
} else {
$prevWeekday = date('l jS \of F Y h:i:s A', strtotime($date . '-1 day'));
}
echo $prevWeekday;

Categories