PHP - strtotime() and datetime strange behavior - php

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.

Related

String to date conversion issue in PHP5.6 [duplicate]

This question already has answers here:
Using strtotime for dates before 1970
(7 answers)
Closed 4 years ago.
date('m/d/Y', strtotime('7-Jan-69'))
It gives output as 01/07/2069, Where
date('m/d/Y', strtotime('7-Jan-75'))
This gives output as 01/07/1975, Why is so and what is the catch?
From the docs:
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC)
Any date before 1970 will be understand as date after 1970
do you need something like this?
<?
// function to convert string and print
function convertString ($date)
{
// convert date and time to seconds
$sec = strtotime($date);
// convert seconds into a specific format
$date = date("Y-m-d H:i", $sec);
// append seconds to the date and time
$date = $date . ":00";
// print final date and time
echo $date;
}
// Driver code
$date = "06/12/2014 04:13 PM";
convertString($date);
?>
To fix that you can use DateTime instead of strtotime() like below,
<?php
$date = $dt = new DateTime('7-Jan-75');
echo $date->format('m/d/Y');
?>
Reason for not working in your case with strtotime:
If the number of the year is specified in a two digit format, the values between 00-69 are mapped to 2000-2069 and 70-99 to 1970-1999.
See the notes below for possible differences on 32bit systems
(possible dates might end on 2038-01-19 03:14:07).
DEMO: https://3v4l.org/d8eoK

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 date year format

I am having problems with dates in php- sometimes the date gets to us in d/m/y and other times its d/m/Y. I want to convert all dates to d/m/Y.
Working with my current dataset, how would I get 24/06/2015 from 24/06/15 using php?
So far I have tried :
$original_date = '24/06/15';
$new_date = date('d/m/Y', strtotime($original_date));
This brings back 01/01/1970
This is probably the most robust method:
$string = '24/06/15';
$date = DateTime::createFromFormat('d/m/y', $string) ?: DateTime::createFromFormat('d/m/Y', $string);
echo $date->format('d/m/Y');
createFromFormat returns false if you try to parse 24/06/2014 using the d/m/y format, so in that case you just retry with d/m/Y. You then get a DateTime object which you can format and output any way you like.
use the lowercase 'y'. See the PHP date manual.
$new_date = date('d/m/y', strtotime($original_date));
y = A two digit representation of a year
The problem is that the strtotime doesn't recognise the UK date format, so convert the format first then format the date.
Try this:
$original_date = "24/06/15";
list($date,$month,$year) = sscanf($original_date, "%d/%d/%d");
$date_convert = $year."-".$month."-".$date;
$new_date = date("d/m/Y", strtotime($date_convert));
echo $new_date;
Its wrong format of date you are using for strtotime.
Have a look at Date Formats
The correct code should have
$original_date = '15/06/24'; // Notice : its mm/dd/yy here
$new_date = date('d/m/Y', strtotime($original_date));

Date from database returns in a different form

I'm trying to get today's date, and compare it to the date in my database, but the date in my database returns in a different form from the date that I get from the date function, so if I compare them in an if statement, the values are always going to be false. Is there a way for me to compare them so that it returns as true?
$date = date('y-m-d'); //date from date function ---> 15/07/19
$dateFromDatabase; //date from database ---> 2015/07/19
if ($date == $dateFromDatabase) {
echo "It's the same day.";
}
strtotime
You have in php some great functions to convert your human readable dates to timestamps.
The first magic function is called strtotime (string to time) : give it your date, you get a UNIX timestamp! Let's see some examples:
echo strtotime('2008-04-12 13:24');
echo strtotime('12 april 2008 13:24');
echo strtotime('12.04.2008 13:24');
And more powerfull, strtotime can recognize some keywords:
echo strtotime('now');
echo strtotime('+4 days');
echo strtotime('+1 month');
echo strtotime('next monday');
echo strtotime('+2 weeks 3 days 4 hours 23 minutes');
The second argument of strtotime is a timestamp, and its default value is the actual timestamp (time()). So echo strtotime('+4 days') is relative to the current time. Of course you can also give strtotime your mysql date! (Note you can also use the mysql function UNIX_TIMESTAMP, which use a bit more ressources).
To compare dates, it's now just a detail:
// your first date coming from a mysql database (date fields)
$dateA = '2008-03-01 13:34';
// your second date coming from a mysql database (date fields)
$dateB = '2007-04-14 15:23';
if(strtotime($dateA) > strtotime($dateB)){
// bla bla
}
Better than substring, isn't it ?!
Here is just another example, not relative to current date but to a particular date:
strtotime('23 hours ago', strtotime('2005-04-13 14:00'));
This mean 23 hours ago relatively to the second given date, which must be a timestamp.
user manual doesn't give a complete description of the supported date formats. Strtotime('dd/mm/YYYY') doesn't work, it works only with mm/dd/YYYY format.
date in dd/mm/YYYY format, can be convert it to YYYY-mm-dd by using explode() function, but I think there are better solutions.
$date = '25/05/2010';
$date = str_replace('/', '-', $date);
echo date('Y-m-d', strtotime($date));

How to reformat date in PHP?

I am pulling the dates of various posts from a database. The dates are in the following format:
2009-08-12
Numeric Year - Numeric Month - Numeric Day
How can I reformat these dates to something more user friendly like:
August 12, 2009
Numeric Month Numeric Date, Numeric Year
Assuming that the date gotten from the mysql database is stored in a variable called:
$date = $row['date_selected'];
Unlike the strtotime based examples, this allows you to ensure the month and day are interpreted in the correct order regardless of locale settings specified on the server.
$date = DateTime::createFromFormat('Y-m-d', '2009-08-12');
$output = $date->format('F j, Y');
date("F d, Y", strtotime($input))
$new_format = date("Your Date String", strtotime($date));
See:
- http://php.net/strtotime
- http://php.net/date
Basically, if strtotime() can read it correctly, you can reformat it anyway you please.
In this case, Year - Month - Day is a properly recognized strtotime() format, this might not be the case for other formats.
You might consider doing your date formatting in MySQL with your select statement:
DATE_FORMAT(date,'%M %e, %Y') as date_selected
http://www.w3schools.com/sql/func_date_format.asp
<?php
echo date('F j, Y', strtotime($date));
You might want to look at the php function strtotime:
http://php.net/manual/en/function.strtotime.php
It'll parse a large number of date representations to a Unix timestamp.
Then use the date function.
Using strtodate or explode to split the date into its different components, you can then use the date function with the appropriate format string:http://php.net/manual/en/function.date.php
$date = "2009-08-12";
list($year,$month,$day) = explode("-",$date);
$formattedDate = date("F d, Y", mktime(0,0,0,$month,$day,$year));
Outputs: "August 12, 2009"
<?php
//Date Formatter
/*
date: date you want to convert
format: its current format ie m-d-Y, m/d/Y, Y-m-d, Y/m/d
delimS: Current delimiter ie - or / or .
delimF: The delimiter you want for the result
NOTE: this will only convert m-d-Y to Y-m-d and back
*/
function dtform($date,$format,$delimS,$delimF){
$dateFinal = '';
if($format == 'm'.$delimS.'d'.$delimS.'Y'){
$dateFinal_exp = explode($delimS,$date);
$dateFinal = $dateFinal_exp[2].$delimF.$dateFinal_exp[0].$delimF.$dateFinal_exp[1];
}else if($format == 'Y'.$delimS.'m'.$delimS.'d'){
$dateFinal_exp = explode($delimS,$date);
$dateFinal = $dateFinal_exp[1].$delimF.$dateFinal_exp[2].$delimF.$dateFinal_exp[0];
}
return $dateFinal;
}
?>
Use it like this:
// February 1, 2005
print date ("F j, Y", mktime (0,0,0,14,1,2004));

Categories