How to convert the ISO date in mongodb:
ISODate("2015-11-20T10:00:09.809Z")
to php date ?
You should check MongoDate:
//build a MongoDate object from a string format
$mongoDate = new MongoDate( strtotime("2010-01-15 00:00:00") );
Once you have a MongoDate object (this is probably your case), you can convert it to a DateTime object this way:
//get a DateTime object
$phpDate = $mongoDate->toDateTime();
And finally convert it to the format you want:
$phpDate->format('M-d-Y');
$date = '2011-09-02T18:00:00';
$time = strtotime($date);
$fixed = date('l, F jS Y \a\t g:ia', $time);
when you extract ISODate("2015-11-20T10:00:09.809Z") from MongoDB
you will get two different timestamps for date=>sec and time=>usec
Example:
"created_at":{"sec":1592249762,"usec":53000}
Then you can add both sec and usec and you will get new timestamp.
$newTimeStamp = $created_at->sec+$created_at->usec;
echo date('Y-m-d H:i:s', $newTimeStamp);
Related
I have a date, in the format yyyy-mm-dd that has been pulled from a MySQL database. This date is saved to a variable $myDate.
I want to save the date, three weeks before this variable's date, to a new variable called $otherDate. How can I do this (and ideally, change the format of the date at the same time)?
I tried:
$otherDate = date("l d F", strtotime("-3 weeks", $myDate));
but to no avail.
The best way to get your head around strtotime is to look at the doc page for it:
The format expected is int strtotime ( string $time [, int $now = time() ] )
And the parameters are defined as:
time
A date/time string. Valid formats are explained in Date and Time Formats.
now
The timestamp which is used as a base for the calculation of relative dates.
Therefore $otherDate = date("l d F", strtotime("-3 weeks", $myDate)); is not correct since $myDate is not an integer but a string in yyyy-mm-dd format.
You must pass the $myDate into the string with the -3 weeks modifier:
$otherDate = date("l d F", strtotime("$myDate -3 weeks"));
Or if you want to redefine $now within the strtotime parameters:
$otherDate = date("l d F", strtotime("-3 weeks", strtotime($myDate)));
I would however recommend using a DateTime object:
$otherDate = new DateTime($myDate);
$otherDate->sub(new DateInterval('P3W'));
echo $otherDate->format('l d F');
The second argument to strtotime() must be a timestamp, not a string, so
$otherDate = date("l d F", strtotime("-3 weeks", strtotime($myDate)));
But look at using DateTime objects instead
$date = new \DateTime( $myDate );
$otherDate = clone $date;
$otherDate->sub( new \DateInterval('P3W') );
echo $otherDate->format( 'l d F' );
Since I find procedural date manipulation ugly and constrictive, I'm going to suggest using PHP's awesome DateTime class.
$dt = new DateTime('2014-11-28');
$dt->modify('-3 weeks');
echo $dt->format('l d F');
Because you are using the strtotime wrong way.
Check this:
$otherDate = date("l d F", strtotime("2014-11-28 -3 weeks"));
echo $otherDate;
Output:
Friday 07 November
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
I have a forum in PHP which takes a date like in the form
dd/mm/yyyy hh:mm:ss. However, I need to insert it for SQL as a DATETIME in the format as yyyy-mm-dd hh:mm:ss. How can I convert this data?
Your date time format is wrong: dd/mm/yyyy hh:mm:ss. Probably you mean d/m/Y H:i:s
If you have 5.3+ version there is safe way to convert the date time into another format. Here's an example:
$timestamp = '31/05/2001 12:22:56';
$timestamp = DateTime::createFromFormat('d/m/Y H:i:s', $timestamp);
echo $timestamp->format('Y-m-d H:i:s');
or if you like more procedural way:
$timestamp = '31/05/2001 12:22:56';
$timestamp = date_create_from_format('d/m/Y H:i:s', $timestamp);
echo date_format($timestamp, 'Y-m-d H:i:s');
Be careful with previous suggestions. Some are completely wrong and others could lead to errors.
You can use the strtotime and date to rework the format.
$new_date = date( "Y-m-d H:i:s", strtotime( $old_date ) );
What this does is take your old date (dd/mm/yyyy hh:mm:ss), converts it to a unix timestamp that can then be used with the php date function to format the date to the desired format.
Two of several possible ways:
Convert in code and then pass the converted value to mysql: $mysqldate = date( 'Y-m-d H:i:s', $phpdate );
Let mysql do the work by using its built-in functions:
$query = "UPDATE table SET datetimefield = FROM_UNIXTIME($phpdate) ...";
if you have datetime avaialable from a from like above format then u just need to use following function.
function localToMysql($dateTime){
$date_chunks = explode('/', $dateTime);
$time_chunks = explode(' ', $date_chunks[2]);
$final_format = $time_chunks[0] . "-" . $date_chunks[1] . "-" . $date_chunks[0] . " " . $time_chunks[1];
return $final_format;
}
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
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)