This question already has answers here:
Accessing dates in PHP beyond 2038
(5 answers)
Closed 5 years ago.
When I try to convert very high dates, such as 2045-01-01, I get another date:
date("Ymd", strtotime("2045-02-15"));
I obtain a wrong date
19700101
but when
date("Ymd", strtotime("2017-02-15"));
I have the good date
20170215
I don't understand why? Someone just explain to me what's going on?
that's a unixtimestamp problem (=> https://de.wikipedia.org/wiki/Unixzeit#/media/File:Year_2038_problem.gif), better:
date_parse("2006-12-12 10:00:00");
date_parse_from_format ( 'Ymd' , "2017-02-15" );
or
$date = DateTime::createFromFormat('Ymd', "2017-02-15");
echo $date->format('Y-m-d');
Related
This question already has answers here:
Subtracting two dates in php
(7 answers)
Closed 8 months ago.
I'm trying to make a program that subtracts the user's inputted birthdate and the date today.
Here is my code:
$bday = $_POST['bday']; //user input
$today=date("Y-m-d");
$myage= $today - $bday; //i got a warning message here saying "A non-numeric value encountered"
Any idea how to fix this?
Please try this snippet, Assuming you are requiring days and/or years as a result. I used the date_diff function for this code. Converting the user input into date/time( has the Y-m-d format) also I hard coded the user input for easier visualisation.
Working Snippet:
$input="2013-12-12"; #User input
$date = strtotime($input); #Convert user input to date time
$date1=date_create(date("Y-m-d")); #date today
$date2=date_create(date('Y-m-d', $date)); #inputted date
$diff=date_diff($date1,$date2); #date_diff function
echo $diff->format("%Y years or %a days");
Output:
8 years or 3123 days
There is possibly a cleaner version of this. But I believe this is the right flow for your requirements.
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
I have a string like this "20180720171534449" which is a kind of time stamp, is there an easy way I can convert this using PHP and format it as a date or date and time that makes sense to a human?
TIA
Peter
You have an 'YmdHisv' format where v is miliseconds.
Miliseconds is not parsable (as I found out today) with date_create_from_format so you need to remove that first from the string with substr.
$s = "20180720171534449";
$date = date_create_from_format('YmdHis', substr($s,0,-3));
echo date_format($date, 'Y-m-d H:i:s'); //2018-07-20 17:15:34
https://3v4l.org/m1XNd
As Ghost pointed out milliseconds is parasble if using microseconds u instead.
$s = "20180720171534449";
$date = date_create_from_format('YmdHisu', $s);
echo date_format($date, 'Y-m-d H:i:s\.v'); //2018-07-20 17:15:34.449
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I'm having a date format like this:
2010-09-21T00:00:00+03:00
how can i convert it to 'Y-m-d H:i:s' format?
I've tried following code but I don't think it's working:
$date = new DateTime('2010-09-21T20:00:00+03:00');
$status_date = $date->format('Y-m-d H:i:s');
It prints time like this: 2010-09-21 20:00:00
It prints exactly want you want ! So what's wrong ?
You can also use Carbon and to like this :
Carbon::parse('2010-09-21T00:00:00+03:00')->toDateTimeString();
UPDATE
Be careful, you said you want midnight but you wrote 8pm in your php !
This question already has answers here:
Add a 1-hour expiration to the current time/date
(5 answers)
Closed 7 years ago.
Hello I have the following php code:
$date = gmdate("Y-m-d\TH:i:s\Z"); /*Format: '2015-05-24T00:00:00Z'*/
How would I simply add 1 month to the above date while keeping the same format?
I tried a few things that lead to nowhere, I am sure there should be a way of doing it that I can't seem to find.
Any help is much appreciated!
Thanks,
Al
try below:
$date = gmdate("Y-m-d\TH:i:s\Z", strtotime("+1 month"));
Try with -
$date = gmdate("Y-m-d\TH:i:s\Z", strtotime('+ 1 MONTH'));
echo $date;
It will add one month to the date.
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
I want to change format of date by 2013-08-09 to 09-Aug-2013, I tried this:
echo $date=date("Y-m-d");
//output 2013-08-09
echo date("d-M-Y",mktime(0-0-0,$date));
but this code showing 10-Aug-2013
I dont know why this showing date 10 instead of 09.
Answer would be highly appreciated , thanks in advance
following is easier:
echo date("d-M-Y",strtotime($date));
at mktime you have to subtract one day
You aren't passing in the parameters to mktime properly. mktime requires 6 integer parameters where as you have 1 integer and 1 string here.
Try using a DateTime object instead:
$date = new DateTime();
echo $date->format('d-M-Y');