strtotime has issues because of date() - php

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

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

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;

Convert 2017-03-01 to March 1st 2017 format inPHP

How to Convert 2017-03-01 to March 1st 2017 format in PHP.
$mark_entry_last_date=$exam_dates['last_date'];
echo date('M j<\sup>S</\sup> Y', $mark_entry_last_date);
Above code outputs current date only but I need the date value for $mark_entry_last_date. How to get a result like March 1st 2017 for given date using escape characters. ?
date function expects second parameter to be timestamp (integer) not string, so you need to use strtotime:
echo date('M j<\s\up>S</\s\up> Y', strtotime($mark_entry_last_date));
Also you need to escape u for sup tag. Since u is format for displaying microseconds.
Try without tags and use strtotime()
echo date("F jS Y", strtotime("2017-03-01"));
// output March 1st 2017
For more http://php.net/manual/en/function.strtotime.php

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

PHP How To Get Values From Dates

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

Categories