Ho do I convert:
2010-12-24 11:39:43
to:
24/12 11:39
Thanks.
This should to the trick:
$newFormat = Date ( 'd/m H:i', StrToTime ( '2010-12-24 11:39:43' ) );
You use StrToTime to convert a string representation of a date to timestamp. You then feed that timestamp to the Date function that takes the format of the date as the first parameter.
Try:
$unixtime = strtotime("2010-12-24 11:39:43");
$newFormat = date("d/m H:i", $unixtime);
echo date("d/m H:i", strtotime("2010-12-24 11:39:43"));
$date = DateTime::createFromFormat('Y-m-d G:i:s', 2010-12-24 11:39:43); //You can simply tell DateTime accept your timestamp as is since PHP 5.3
echo $date->format('d/m G:i T'); //Will output what you wanted + Timezone Abbreviation (because of the T)
Related
How can I just convert date part of datetime to timestamp?
For example in this datetime: 2018-02-26 20:30:00
I want timestamp of 2018-02-26 00:00:00.
If you prefer using the DateTime API you could also do:
$dateAndTime = '2018-02-26 20:30:30';
DateTime::createFromFormat('Y-m-d H:i:s', $dateAndTime)->setTime(0, 0)->getTimestamp();
If you use substr you only need to use strtotime once and no date.
Echo strtotime(substr("2018-02-26 20:30:00",0,10));
Or you can use explode:
Echo strtotime(explode(" ", "2018-02-26 20:30:00")[0]);
I explode on space and use the first item [0]
<?php
echo strtotime(date("Y-m-d",strtotime("2018-02-26 20:30:00"))." 00:00:00");
Here you go. Just use strtotime twice. Once to set your date and then the second to generate the timestamp.
strtotime() can be used to convert just about any human readable date string to a timestamp.
http://php.net/manual/en/function.strtotime.php
$str = '2018-02-26 20:30:30';
echo date('Y-m-d', strtotime($str)) . '<br>';
echo strtotime(date('Y-m-d', strtotime($str)));
An easy and best solution would be this one:
$datetime = '2018-02-26 20:30:00';
$new_date = strtotime( Date( 'Y-m-d', strtotime( $datetime ) ) );
Try this:
$date = "2018-02-26 20:30:00";
echo strtotime(date('Y-m-d 00:00:00', $date));
This outputs UNIX timestamp. You can remove strtotime to get gregorian calendar
Im trying to add a certain amount of days to a timestmp using this in PHP:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo $endDate2;
but its displaying: 1216526400
any ideas?
Try:
echo date("Y-m-d H:i:s",$endDate2);
Or (for just the date):
echo date("Y-m-d",$endDate2);
You can find documentation about how to format your string here: http://php.net/manual/en/function.date.php
You should be using DateTime for working with dates. It's timezone friendly.
$datetime = new DateTime('2008-06-20');
$datetime->modify('+1 day');
echo $datetime->getTimestamp();
strtotime() converts the date into a unix timestamp which is the number of seconds since January 1st 1970. If you want a date output you have to run the finished timestamp through date() first.
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate.' +1 day');
echo date("Y-m-d", $endDate);
strtotime creates a Unix timestamp so if you want to be presented with a formatted date, you need to pass the timestamp as an argument to the date function as follows:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo date('Y-m-d', $endDate2);
Additionally, there are a wide variety of parameters you can use in the date function if you want to display additional information.
e.g.: echo date('Y-m-d H:i:s', $endDate2); or echo date('Y-m-d h:i:s a', $endDate2);, etc.
Sooooo close, just take your timestamp and convert it back into date format using date("desired format",$endDate2);
DateTime is a very nice way to deal with dates. You can try like this:
$capturedDate = '2008-06-20';
$date = DateTime::createFromFormat('Y-m-d', $capturedDate)->modify('+1 day');
echo $date->getTimestamp();
how would you convert a date stored as
2011-01-18 11:51:41
into
18-01-2011 11:51:41
using PHP?
many thanks in advance!
date('d-m-Y H:i:s', strtotime('2011-01-18 11:51:41'));
More reliable than using strtotime(), assuming you're on PHP 5.3+
$oldtime = date_parse_from_format('Y-m-d h:i:s', '2011-01-18 11:51:41');
$newtime = date('d-m-Y h:i:s', $time);
However, the date format you're converting FROM suggests it's coming from a MySQL datetime field, in which case you could also do:
SELECT DATE_FORMAT(yourfield, '%d-%m-%Y %H:%i:%s')
and save yourself a full roundtrip in PHP.
Convert the old date to UNIX time with strtotime(), then output it in the new format with date()
$olddate = "2011-01-18 11:51:41";
$newdate = date('d-m-Y H:i:s', strtotime($olddate));
echo $newdate;
// 18-01-2011 11:51:41
$your_date = "2011-01-18 11:51:41";
echo date('d-m-Y H:i:s', strtotime($your_date));
demo
i need to convert strtotime to date btime format (from 1307595105 to 06/08/2011 09:51:45 PM PDT) in php
Could you please give me an answer
$unixtime = 1307595105;
echo $time = date("m/d/Y h:i:s A T",$unixtime);
Where
http://php.net/manual/en/function.date.php
<?php
echo date('d - m - Y',strtotime('2013-01-19 01:23:42'));
?>
Out put : 19 - 01 - 2013
Use date() function to get the desired date
<?php
// set default timezone
date_default_timezone_set('UTC');
//define date and time
$strtotime = 1307595105;
// output
echo date('d M Y H:i:s',$strtotime);
// more formats
echo date('c',$strtotime); // ISO 8601 format
echo date('r',$strtotime); // RFC 2822 format
?>
Recommended online tool for strtotime to date conversion:
http://freeonlinetools24.com/
FORMAT DATE STRTOTIME OR TIME STRING TO DATE FORMAT
$unixtime = 1307595105;
function formatdate($unixtime)
{
return $time = date("m/d/Y h:i:s",$unixtime);
}
Here is exp.
$date_search_strtotime = strtotime(date("Y-m-d"));
echo 'Now strtotime date : '.$date_search_strtotime;
echo '<br>';
echo 'Now date from strtotime : '.date('Y-m-d',$date_search_strtotime);
I am using the date() function to display a timestamp from my databse.
$date = date( 'F jS', $news_items['date']);
I know the $news_items['date']; as they return in a YYYY-MM-DD 00:00:00 format.
But after the function call $date dispays as December 31st for all values.
$date = date('F jS', strtotime($news_items['date']));
Try that :)
That's because date() wants a timestamp and not a string. You're better of with something like this;
$dt = date_create($news_items['date']);
$date = date_format($dt, 'F jS');
Or in the object oriented way;
$dt = new DateTime($news_items['date']);
$date = $dt->format('F jS');
The correct syntax for date function is given in PHP manual as:
string date ( string $format [, int $timestamp ] )
As you can see the second argument is expected to be an integer, but you are feeding the function with a string.