This question already has answers here:
Using strtotime for dates before 1970
(7 answers)
Closed 9 years ago.
I am trying to separate time from the given string date & time. I simply tried with following example.
$time = '2014-01-20 10:45:45';
echo '<br>'.$finalTime = date("H:i:s",strtotime($time));
echo '<br>'.$date = date("d F Y",strtotime($time));
I am getting correct date and time.
10:45:45
20 January 2014
But when I tried with given string, no correct result.
$time = '1899-12-30 19:30:00';
echo '<br>'.$finalTime = date("H:i:s",strtotime($time));
echo '<br>'.$date = date("d F Y",strtotime($time));
PHP is always returning me following result.
00:00:00
01 January 1970
I am not sure whether is there any limitation on date function that is not returning 1899. Is that so?
Your date is before the unix epoch. DateTime() allows you to work around that.
$dt = new DateTime("1899-12-30 19:30:00");
echo $dt->format("d F Y");
echo $dt->format("h:i:s");
Use DateTime and DateTime::format():
$time = '1899-12-30 19:30:00';
$dt = new DateTime($time);
echo $dt->format('d F Y H:i:s');
Working example: http://3v4l.org/fM22Z
The strtotime() method is limited by the Unix epoch, which is Jan 1, 1970.
Update: As Mark comments below, your code would work all the way back to 1901 (as a negative timestamp), see here: http://3v4l.org/CSJte
Related
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
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I like to convert a date format dd/mm/yyyy to another format in PHP.
My Code:
$date = '29/01/2018';
echo date('l jS F Y', strtotime($date));
When I run the above code, it's showing me some wrong date:
Thursday 1st January 1970
Am I doing anything wrong?
You could also use the DateTime objects to help you converting dates. You can "create a datetime object from an specified format" and convert it.
$date = "29/01/2018";
$dt = DateTime::createFromFormat("d/m/Y", $date);
echo $dt->format("l jS F Y");
signifies American M/D/Y formatting
<?php
$date = '01/29/2018';
echo date('l jS F Y', strtotime($date));
output
Monday 29th January 2018
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
i want to convert a date format to another using php
6th of November 2017 to 2017-11-6
i tried
$newDate = date("Y-m-d", strtotime('6th of November 2017'));
but it always returns 01-01-1970
When you create date from specific format then use createFromFormat of date.
$date = DateTime::createFromFormat('jS \o\f F Y', '6th of November 2017');
echo $date->format('Y-m-d');
Note: If you have string in your format then you have to escape that string (with \) for conversation
DEMO
Replace 6th of to 6th and it will be fine..
$newDate = date("Y-m-d", strtotime('6th November 2017'));
echo ($newDate);
result
2017-11-06
Try this:
$newDate = date("Y-m-d", strtotime("6 November 2017"));
This question already has answers here:
PHP date format conversion
(3 answers)
Closed 9 years ago.
Is it possible through PHP to change the format of this date
Jan 1 1900 12:00AM
to
01/01/1990
mm/dd/yyy
Two functions will help: strtotime and date.
So do this:
date("m/d/Y", strtotime("Jan 1 1990 12:00AM"))
See PHP's excellent DateTime classes:-
$date = DateTime::createFromFormat('M d Y H:ia', 'Jan 1 1900 12:00AM');
echo $date->format('m/d/Y');
Or, if you are a fan of one liners:-
echo DateTime::createFromFormat('M d Y H:ia', 'Jan 1 1900 12:00AM')->format('m/d/Y');
You could parse that into a timestamp with strtotime() and then use the PHP date() function to format it.
strtotime and strftime are the easiest to work with.
$stamp = strtotime('Jan 1 1900 12:00AM');
echo strftime("%m/%d/%G", $stamp);
See the full strftime documentation to output your date in any format.
No, because Jan 1 1900 1200AM is never going to equal 01/01/1990. Check you year. But if you want a simple what to convert string to time try this:
<?php
$d = 'Jan 1, 1900 12:00AM';
echo date("m/d/y",strtotime($d));
?>
I would pass this into a function first that removed the time element and then converts only the data portion.
see the date function:
//replace the time() with yours
echo date('m/d/Y', time()); // = 06/06/2013
manual: http://php.net/manual/de/function.date.php
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to convert date to timestamp in PHP?
I want to transfer some date to timestamp ,the date formart is %day %month %time. Like 25 nov 07:44, I tried some method below, but the two dates are different. need a help, thanks.
<?php
$date = '25 nov 07:44';
$time = explode(" ",$date);
$minute = explode(":",$time[2]);
$timestamp = mktime(''.$minute[0].' '.$minute[1].' 00 '.$time[1].' '.$time[0].' 2011')."<br />";// mktime(%hour, %minute, %second, %month, %day, %year);
echo $timestamp; //1322202671
echo date("Y-m-j H:i:s", $timestamp); //2011-11-25 07:31:11
?>
A few problems:
You are feeding mktime a string instead of a comma separated list
$time[1] is a string and mktime needs a number for the month
You might want to give it a try with strtotime or otherwise add a translation table to go from your month strings to a number.
In recent versions of php (5.3+), you can use DateTime::createFromFormat().
Try something like
$format = 'd M H:i';
$date = DateTime::createFromFormat($format, $timestamp);
echo $date->format('Y-m-j H:i:s');
Personally, I find this class based approach a little cleaner.
PHP Docs:
DateTime::createFromFormat
date() for info on format strings