PHP How To Get Values From Dates - php

Would be grateful for any assistance with this.
I have:
$value = gmdate("F d Y", getlastmod());
This is returning (for example):-
June 24 2016
My question is how to extract the dayofweek information out of $value?
My output needs to look like:
Friday, June 24, 2016
Is there a way to extract the dayofweek, Month, dayofmonth and year out of $value? If not, how can I get those values from 'getlastmod' ?
Many thanks

First you will need to use strtotime to convert your current $value to timestamp.
Then by using the date()function you can get the day of the week.
Example:
<?php
echo date("l", strtotime("June 24 2016")); // Output: Friday

Try this instead:
$value = gmdate("l, F d, Y", getlastmod());
Output:
Friday, June 24, 2016

$myDate = date_create(getlastmod());
echo $myDate->format('L, F d, Y');
This will print Friday, June 24, 2016
More on date/time formats here:
http://php.net/manual/en/function.date.php

From the PHP formatting guide for date (gmdate is the same as date except it is using GMT) you can use "l" to get the full text of the day of the week.
http://php.net/manual/en/function.date.php
So in your example I think the following should work to meet your output needs.
$value=gmdate("l, F d, Y",getlastmod());

Related

How to display month (as string) and year (in 4 digits) from particular date from table?

I want to display my date as "Jan 2017" and "Jun 2017" from the dates 01-01-2017 and "01-06-2017"
You can use strtotime() and date() function for this.
Example:
echo Date('M Y', strtotime("01-01-2017"));
echo "\n";
echo Date('M Y', strtotime("01-06-2017"));
Output:
Jan 2017
Jun 2017
You should use date_format() function, to directly fetch dates in required format. For your required format, you can use the following query:
select date_format(now(),'%b-%y');
Replace now() with your column name
Refer this link for more details and formatting instructions

Display todays day of the week, month, day, year in THIS format in PHP?

I would like to display this format of the current date using PHP. I have googled and found a few variances of what I want to do but the company I work for wants it specifically in this format to match what they've mailed out.
Monday, March 16, 2015
Thanks.
If you looked at the PHP documentation for date you'd be able to figure it out very easily:
echo date('l, F j, Y');
which outputs
Monday, March 16, 2015
As mentioned by #alfallouji, you should have a look at the php documentation.
Here is what you need:
$now = new \DateTime();
echo $now->format('l, F j, Y');
The date format string should be "l, F d, Y".
You can display current date in this format as below:
<?php
echo date("l, F d, Y");
?>

strtotime has issues because of date()

i have a problem with news script.
the code below will generate today's date, i think it's because that i have date() in the beggining, or maybe the line is just not written right.
the $postdate in the () is a numeric date, such as 13.11.01, again, i get the same date of today on all of the posts in the loop.
what's wrong with my line?
i need it to output the date as text one. such as December 13th, 2013
thanks
<?
$postdate = date('F jS, Y', strtotime($postdate));
?>
The problem is likely that strtotime() doesn't recognize the . character as a valid separator. See below:
Strtotime() doesn't work with dd/mm/YYYY format
First you have a typo on your question.
13.11.01 is November 1st, 2013 , Not November 13th, 2013
So, go the OOPway like this.
<?php
$dt='13.11.01';
$date = DateTime::createFromFormat('y.m.d', $dt);
echo $datex= $date->format('F jS, Y'); //"prints" November 1st, 2013

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

PHP date difference extremly hard to accomplish

I have to get back dates just subtracting days from the current date and time.
Let suppose current date is May 1, 2011
after subtracting 30 days
April 1, 2011
How to accomplish this in PHP? Please help
$unixtime = strtotime("May 1, 2011 -30 days");
$human_readable = date('F j, Y', $unixtime);
Elaborating on Emil's answer. You can one-line it like so:
$thirty_days_ago = date('F j, Y', strtotime('-30 days'));
If you leave out the "May 1, 2011" part in strtotime(), you'll get 30 days ago from whatever the current date is.
Also, you've got a number of options for formatting in the docs.

Categories