my date format from my db was Y-m-d h:i:s sample: 2017-08-30 19:19:20 and im trying to display that date on my page..
but the problem is when it displays on my page the date was different compared to the data from my db..
like
data from db was
2017-08-21 18:00:00
2017-08-24 18:00:00
2017-08-30 18:00:00
but the date that was being displayed was
12/31/1969 6:00 PM
12/31/1969 6:00 PM
12/31/1969 6:00 PM
this is the format that i use..
<?php echo date('m/d/Y H:i a',strtotime($date)); ?>
$date is the date from my db..
the date should be
08/21/2017 6:00 PM
08/24/2017 6:00 PM
08/30/2017 6:00 PM
any idea why this happens and any possible solution for this?..
The given lines of php code should be able to give the required output :-
<?php
$dates = [
'2017-08-21 18:00:00',
'2017-08-24 18:00:00',
'2017-08-30 18:00:00',
];
foreach ($dates as $string) {
$date = new DateTime($string);
echo $date->format('m/d/Y g:i A')."\n";
}
Output:
08/21/2017 6:00 PM
08/24/2017 6:00 PM
08/30/2017 6:00 PM
The code snippet is taken from the 3v4l.org.
The other answers seems to answer how to fix it.
I thought I could explain why it happens.
Strtotime fails because it can't parse the date and returns false.
False is the same as 0.
This zero is taken by date and creates a date according to your format.
But because strtotime returned 0 the date returns 0 UNIX time => 1970-01-01 00:00.
And since you are in a timezone (or server at least) with negative difference to UTC a few hours are subtracted from 1970-01-01 00:00 and that leaves you with your output.
Related
How to convert 10/25/2014 14:00 PM to 2014-10-25 14:00:00 in php?
date('Y-m-d H:i:s', strtotime("10/25/2014 14:00 PM"));
returns
1970-01-01 03:00:00
I don't know what the origin of that date is (how it came to that), but you can use DateTime classes for this:
$raw_date = '10/25/2014 14:00 PM';
// to disregard that PM escape it in the format
$new_date = DateTime::createFromFormat('m/d/Y H:i \P\M', $raw_date);
echo $new_date->format('Y-m-d H:i:s'); // 2014-10-25 14:00:00
$date = "10/25/2014 14:00 PM"; //Here is the date 24 hours format with am/pm
$date = substr($date, 0, -2); //Removed the am/pm from date
echo date('Y-m-d H:i:s', strtotime($date)); //then convert it to mysql date format
Note:
If you use this for date column means replace it data type to int and save unix time stamp which have more comfortable if year limited to 1970 to year 2038.
For reason not to use more than 2038 read here
10/25/2014 14:00 PM your input date format is not correct, you are using 12hr format so time will be 10/25/2014 02:00 PM
My database has date entries like this:
29th August 20:00 PM
echo $datetest;
//prints 29th August 20:00 PM
$myDateTime = DateTime::createFromFormat('jS F H:i A', $datetest)
$startHoursString = $myDateTime->format('H');
echo $startHoursString;
// prints 08 instead of 20
I was expecting that $startHoursString would be 20 instead of 8.
Could anyone shed some light on this or a workaround?
It is dirty hack, but it should work, if you are sure that dates are in this AM/PM format, you'll have to remove last 3 characters from the string (AM or PM plus space):
$myDateTime = DateTime::createFromFormat('jS F H:i', substr($datetest, 0, -3));
Fiddle here.
Also, remember that using AM/PM is valid for up to 12 Hour, 20:00 AM/PM is INVALID format.
i was fetching this date from table in the database like this format
Sunday 16th of January 2011 06:55:41 PM
and i want to convert it to be like this format
11-05-2012
how to do that with date function or any function
when i use date function
<td><?php echo date('d-m-Y', $v['v_created']); ?></td>
i get error message
'Severity: Warning
Message: date() expects parameter 2 to be long, string given'
This works for me (just tested on local web server)
<?php
date_default_timezone_set ('Europe/Rome');
$date = "Sunday 16th of January 2011 06:55:41 PM";
//.Strip "of" messing with php strtotime
$date = str_replace('of', '', $date);
$sql_friendly_date = date('y-m-d H:i', strtotime($date));
echo $sql_friendly_date;
?>
You can format the date as you prefer changing the first parameter of Date function according to: http://it2.php.net/manual/en/function.date.php
You have the following format:
Sunday 16th of January 2011 06:55:41 PM
that is a string based format, so the date information is more or less encoded in a human readable format. Luckily in english language. Let's see, that are multiple things all separated by a space:
Sunday - Weekdayname
16th - Date of Month, numeric, st/nd/th form
of - The string "of".
January - Monthname
2011 - Year, 4 digits
06:55:41 - Hour 2 digits 12 hours; Colon; Minute 2 digits; Colon; Seconds 2 digits
PM - AM/PM
So you could separate each node by space and then analyze the data. All you need is all Monthnames and the sscanf function because you only need to have the month, date of month and year:
$input = 'Sunday 16th of January 2011 06:55:41 PM';
$r = sscanf($input, "%*s %d%*s of %s %d", $day, $monthname, $year);
Which will already give you the following variables:
$monthname - string(7) "January"
$day - int(16)
$year - int(2011)
So all left to do is to transpose the monthname to a number which can be done with a map (in the form of an array in PHP) and some formatted output:
$monthnames = array(
'January' => 1,
# ...
);
printf("%02d-%02d-%04d", $day, $monthnames[$monthname], $year);
So regardless of which problem, as long as the input is somewhat consistently formatted you can pull it apart, process the gained data and do the output according to your needs. That is how it works.
try this. always worked for me
$date = Sunday 16th of January 2011 06:55:41 PM
$new_date = date('d-M-Y', strtotime($date));
echo $new_date;
The format you are using Sunday 16th of January 2011 06:55:41 PM is a wrong format.from the form you are inserted this date in database should be in date(Y-m-d) than the value of date inserted in database like:- 11-05-2012. and you can fetch this and get the format what you want.
<?php
$old_date = date('l, F d y h:i:s'); // returns Saturday, January 30 10 02:06:34
$new_date = date('d-M-Y', strtotime($old_date));
echo $new_date
?>
more details about date plz visit this url
http://www.php.net/manual/en/datetime.createfromformat.php
I don't know why it is so confusing, maybe it s because there are so many ways to output/input date and time with mysql and php. All I want is for users to put a date in this format
MM/DD/YYYY
and for it to output in this format
Month Name - Date
and for time, I want users to pick the time from a select field, starting with 1:00 P.M. and incrementing down 30 minutes, so it will be 1:30 P.M., 2:00 P.M... and I want to somehow insert this into MySQL and output it as 1:30 P.M. Not sure how to do that either.
The best way to store a date format in a database and be able to display it the way you want is to use the time() and date() function.
when storing the date in your database you should use time(), it will generate a string like this -> 1300695900. this little string contains the date and time
then you can use date to display it in any way you want:
$time = time();//you would normally get this from the database
date('d M Y', $time); //outputs 21 Mar 2011
date('m-d-y', $time); // outpus 03-21-2011
and so on...
Edit:
to answer your last question, you just get the different values, stick it together (concatenation) and then use strtotime()
$date = $_POST['date']; // eg 03/03/2011
$time = $_POST['time']; // eg 1:30
$daypart = $_POST['daypart']; // eg PM
$date_time = $date.' '.$time.' '.$daypart;
$strtime = strtotime($date_time);
echo date('d M Y - h:i', $strtime);// outputs 03 Mar 2011 - 01:30
How can I convert times like 2 pm or 3 am to HH:MM:SS format in php?
Like this:
$date = '2pm';
echo date('H:i:s', strtotime($date));
Result:
14:00:00
And:
$date = '2am';
echo date('H:i:s', strtotime($date));
Result:
02:00:00
More Info:
date
strtotime
Update:
To convert it back:
$date = '14:00:00';
echo date('HA', strtotime($date));
Result:
14PM
(this should be a comment, but it's too long)
The reason Sarfraz's solution is incomplete is because it doesn't account for DST transaitions. During DST transitions, some hours may not exist.
Consider the timezone is Europe/Lisbon and we're in March 28th 2010, when DST kicked in.
When we hit 1am, we change from UTC+0 to UTC+1, i.e., we skip 1 hour. Example:
date_default_timezone_set("Europe/Lisbon");
$date = '2010-03-28 1am';
$date2 = '2010-03-28 1:30am';
echo date('H:i:s', strtotime($date)),"\n";
echo date('H:i:s', strtotime($date2)),"\n";
gives
02:00:00
02:30:00
Therefore, Sarfraz solution will fail unless when you say you want to convert 1am to 01:00, these times always refer to the current day in the server's timezone.