I've a date formatted like "Tue Jan 05 11:08:27 +0000 2010" and I want to convert it's format to "yyyy-mm-dd 00:00" in PHP.
How can I do that?
convert it to a PHP date object with strtotime() then output it with date()
EDIT
Some more detail; try:
$time = strtotime('Tue Jan 05 11:08:27 +0000 2010');
echo date("Y-m-d h:i", $time);
Y = 4 digit year
m = 2 digit month (with leading 0)
d = 2 digit month (with leading 0)
h = 12 hour time (leading 0)
i = minutes (with leading 0)
http://php.net/manual/en/function.date.php
for all the formatting options
$time_string = 'Tue Jan 05 11:08:27 +0000 2010';
$formated_time = date('Y-m-d h:i', strtotime($time_string));
echo $formated_time;
strtotime + date
Agree with Erik, if you want to do it in one line.
Solution
$date = date('Y-m-d H:i:s', strtotime('Tue Jan 05 11:08:27 +0000 2010'));
Related
1517317337107
it should be: Wed Jan 31 00:02:17 GMT+11:00 2018
I verify this on https://www.epochconverter.com/
the result is:
Assuming that this timestamp is in milliseconds:
GMT: Tuesday, January 30, 2018 1:02:17.107 PM
Your time zone: Wednesday, January 31, 2018 12:02:17.107 AM GMT+11:00 DST
My code in php is:
$timestamp = 1517317337107;
echo date('Y-m-d H:i:s', $timestamp);
the output is "2038-01-19 04:14:07"
what should I do?
That timestamp is in milliseconds, not seconds. Just divide it by 1000.
php > $timestamp = 1517317337107;
php > echo date('Y-m-d H:i:s', $timestamp/1000);
2018-01-30 05:02:17
Convert Your timestamp in seconds and set timezone
$timestamp = 1517317337107;
$timestamp_in_seconds = $timestamp/1000;
date_default_timezone_set("Australia/Sydney");
echo date('D M d Y H:i:s', $timestamp_in_seconds);
Output: Wed Jan 31 2018 00:02:17
Hi I want change date string -> Sat Oct 04 00:00:00 GMT+05:30 2008 -> into 'Y-m-d H:i:s' format my code is as below :
$lsd = strtotime($vehicle->newDate);
$newDate = date('Y-m-d H:i:s',$lsd);
I am getting date like 1970-01-01 05:30:00 Why is so ?
I have gone through reference : http://php.net/manual/en/function.date.php
I have got date fromat representation labels for all except GMT+05:30
Sat Oct 04 00:00:00 GMT+05:30 2008
D M d H:i:s o
Instead of down-votes if that one label I get then I will be thankful then I will use http://php.net/manual/en/datetime.format.php,
Use this one.
$lsd = strtotime("Sat Oct 04 00:00:00 GMT+05:30 2008");
$newDate = date('Y-m-d H:i:s',$lsd);
echo $newDate;
Try this
$lsd = strtotime($vehicle->newDate);
$newDate = date("D M j G:i:s T Y");
I have a php script that returns the date of the server:
<?php
echo date('D, d M y H:i:s a');
?>
but when I print this value on the client site I get:
Tue, 29 Sep 15 16:19:28 pm
But instead I need the date in this format:
Tue Sep 29 2015 16:18:00 GMT+0200 (Central Europe Daylight Time)
How should I modify my php script then to have it like this?
Thanks!
echo (new DateTime())->format('r');
$datestring = "26-08-2015 03:35:28"; //date as string
$date = new DateTime($datestring); //String to datetime conversion
$date = $date->format('D d M y H:i:s O e'); //format the date
echo $date;
This is the manual link. Timezone you have to set.
Output will look like Wed 26 Aug 15 03:35:28 +0200 Europe/Paris
Go to the very bottom of this page http://php.net/manual/en/function.date-default-timezone-set.php
I am using strtotime for saving date like strtotime($_POST['end_date']) and this give me format 1441785600 that show very fine in google calendar but when I save string like
Thu Sep 03 2015 14:00:00 (24 hour format)
Data stop displaying in google calendar, I want to convert above sample (24Hours) using strtotime and convert it into something like that 1441785600
You may use something like this which currently doesn't give as you want, but may be useful-
$date = 'Sep 03 2015 14:00:00';
$parsedDate = date_parse_from_format('M d Y H:i:s', $date);
$time_stamp = mktime(
$parsedDate['hour'], $parsedDate['minute'], $parsedDate['second'],
date('n', strtotime($parsedDate['month'])), $parsedDate['day'], $parsedDate['year'],
date('I')
);
You could do:
$dateString = 'Thu Sep 03 2015 14:00:00 (24 hour format)';
echo strtotime(preg_replace('~\w+\s(.+)\s\(.+~','\1',trim($dateString)));
I have two queries, both related to dates.
1) I have dates in these formats, which I'm looking to normalise into the same format before saving into a database:
Saturday 26 July
Monday 28 - Wednesday 30 July
July 24th, 2014
Thu 4 Sep
Thu 28 Aug — Fri 19 Sep
24-07-2014
Single days are quite easy to work out using strtotime(), but ranges of dates are a bit more tricky.
This, for example, doesn't work:
$dateString = "Monday 28 - Wednesday 30 July";
if (strpos($dateString, "-")) {
$datePieces = explode("-", $dateString);
$startDate = strtotime($datePieces[0]);
$endDate = strtotime($datePieces[1]);
} else {
$startDate = strtotime($dateString);
$endDate = strtotime($dateString);
}
echo '<pre>';
echo date('d F Y', $startDate);
echo '<br/>';
echo date('d F Y', $endDate);
Because the month is only on one side of the explode(), doing it this way returns:
01 January 1970
30 July 2014
2) I need a way of working out what year the date is (it will always be in the future). Something along the lines of:
if (the month in the date string has elapsed) {
the year of the date is this year + 1
}
As long as each source provides you with a consistent format you can use DateTime() and DateTime::createFromFormat() to process the dates for you.
//Saturday 26 July
$date = DateTime::createFromFormat('l j F', 'Saturday 26 July');
//July 24th, 2014
$date = new DateTime('July 24th, 2014');
//Thu 4 Sep
$date = DateTime::createFromFormat('D j M', 'Thu 4 Sep');
//Thu 28 Aug — Fri 19 Sep
list($start, $end) = explode(' - ', 'Thu 28 Aug — Fri 19 Sep');
$start = DateTime::createFromFormat('D j M', $start);
$end = DateTime::createFromFormat('D j M', $end);
//24-07-2014
$date = new DateTime('24-07-2014');
I'm going to leave handling Monday 28 - Wednesday 30 July to you since you'll need to do a little more work to get the month from the second date and apply it to the first. But this should show you how to go about this.