timespamp convert php - php

How do I convert the standard php timestamp e.g. 1278184362 into this format
2010-07-03 19:00:00 ?
needs to be exact...
Any ideas guys?

You could do it like this:
$timestamp = 1278184362;
$newDate = Date("Y-m-d H:i:s", $timestamp);

Related

How to turn a string like "16/Sep/2014 08:34" to unix timestamp in PHP

Struggling without much success to turn strings like "16/Sep/2014 08:34" extracted from an array with explode command to unix timestamp like "2014-09-17 05:32:05" in PHP. Any help, please?
Edit: With #Erik's help I finally got the right result:
$date = DateTime::createFromFormat("d/M/Y H:i", $line[0]);
$date = $date->format('Y-M-d H:i');
$timestamp = strtotime($date);
You'll need to use DateTime::createFromFormat and then convert the resulting datetime to a timestamp by using $datetime->getTimestamp();
--
// this will create a generic PHP date object, which you can then manipulate into anything you want
$date = DateTime::createFromFormat( "d/M/Y H:i", "16/Sep/2014 08:34" );
// this will generate a unix timestamp (which is an integer)
$timestamp = $date->getTimestamp();
// this will generate the string you request in your question
$string = $date->format( "Y-m-d H:i:s" );
--
For more info on formatting dates, check out the PHP documentation: http://php.net/manual/en/datetime.createfromformat.php

Create a php date format using datestring

I have a string like 2012-11-08.
I want to convert this string into php date format with time too.
The output should be like
$smv = date('Y-m-d H:i:s', time());
print $smv // 2012-11-08 16:05:56 (If we consider India )
I tried
$day = '2012-10-08';
echo = date("Y-m-d H:i:s",strtotime($day));
But it is printing the date as 2012-10-08 00:00:00 (current time is not printed).
Try like this
$day = '2012-10-08'.date('H:i:s', time());
echo date("Y-m-d H:i:s",strtotime($day));

/Date(1341788400000+0100)/ to DD/MM/YYYY HH:MM

I have several dates being outputted into variables. They are formatted as follows:
/Date(1341788400000+0100)/
How would I go about formatting them using PHP into:
DD/MM/YYYY HH:MM
Thanks!
I ended up using the following, as the initial format was in milliseconds:
$date = 1341788400000+0100;
$date = ( $date / 1000 );
$date = date("d/m/Y H:m", $date);
$date = 1341788400000+0100;
echo date("Y/m/d H:m",$date);
Unless the +0100 is the actual time of the day (01:00) ?
First, you parse it, e.g. using strtok() http://php.net/manual/en/function.strtok.php
Then parse it as a number.
$seconds = intval($a)
Then format it using
date("Y/m/d H:m", $seconds)`.

converting between date formats

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

Timestamp to Date

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)

Categories