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

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)`.

Related

How to format string to date

I trying to format string to date. String looks like that: 20200219 and I want to format it like 2020-02-19. Can some help me with this?
You have multiple options:
Use strtotime() on your first date then date('Y-m-d') to convert it back:
$changed_date = "20200219";
echo date("Y-m-d", strtotime($changed_date ) );
$time = strtotime('03/05/2020');
$newformat = date('Y-m-d',$time);
echo $newformat;
// 2020-03-05
You need to be careful with m/d/Y and m-d-Y formats. PHP considers / to mean m/d/Y and - to mean d-m-Y. I would explicitly describe the input format in this case:
$ymd = DateTime::createFromFormat('m-d-Y', '03/05/2020')->format('Y-m-d');
Another Option:
$d = new DateTime('03/05/2020');
$timestamp = $d->getTimestamp(); // Unix timestamp
$formatted_date = $d->format('Y-m-d'); // 2020-03-05
you can do like that
$s = '20200219';
$date = strtotime($s);
echo date('Y-m-d', $date);
if it's a string you could do like this:
$date=date_create("20200219");
return date_format($date,"Y-m-d");
Hope it helped.
Try this one:-
$var = "20200219";
echo date("Y-m-d", strtotime($var) );
You can try php way:
date("Y-m-d", strtotime("20200219") );

PHP date year format

I am having problems with dates in php- sometimes the date gets to us in d/m/y and other times its d/m/Y. I want to convert all dates to d/m/Y.
Working with my current dataset, how would I get 24/06/2015 from 24/06/15 using php?
So far I have tried :
$original_date = '24/06/15';
$new_date = date('d/m/Y', strtotime($original_date));
This brings back 01/01/1970
This is probably the most robust method:
$string = '24/06/15';
$date = DateTime::createFromFormat('d/m/y', $string) ?: DateTime::createFromFormat('d/m/Y', $string);
echo $date->format('d/m/Y');
createFromFormat returns false if you try to parse 24/06/2014 using the d/m/y format, so in that case you just retry with d/m/Y. You then get a DateTime object which you can format and output any way you like.
use the lowercase 'y'. See the PHP date manual.
$new_date = date('d/m/y', strtotime($original_date));
y = A two digit representation of a year
The problem is that the strtotime doesn't recognise the UK date format, so convert the format first then format the date.
Try this:
$original_date = "24/06/15";
list($date,$month,$year) = sscanf($original_date, "%d/%d/%d");
$date_convert = $year."-".$month."-".$date;
$new_date = date("d/m/Y", strtotime($date_convert));
echo $new_date;
Its wrong format of date you are using for strtotime.
Have a look at Date Formats
The correct code should have
$original_date = '15/06/24'; // Notice : its mm/dd/yy here
$new_date = date('d/m/Y', strtotime($original_date));

Formatting date gives 12/31/1969

I'm trying to format a date in the form mm-dd-yyyy to the form yyyy-mm-dd, however, when I try formatting it, it comes out as 1969-12-31.
Here's my code:
$custom_date = "10-13-2013";
$formatted_date = date("Y-m-d", strtotime($custom_date));
What's wrong?
$custom_date = "10-13-2013";
$formatted_date = DateTime::createFromFormat("m-d-Y", $custom_date)->format("Y-m-d");
mm-dd-yyyy is not a format that is recognised by strtotime. That's because it wouldn't reliably be able to handle dates like 03-04-2013, it is the fourth of March or the third of April?
You need to parse it manually, or use the DateTime class.
list($m,$d,$y) = explode("-",$_GET['date']);
$timestamp = mktime(0,0,0,$m,$d,$y);
$formatted_date = date("Y-m-d",$timestamp);

Timestamp to yyyymmdd hhmm but only keep hhmm

I have a timestamp in the database. With the following code I can format it to the right date:
$datefrom=mysql_real_escape_string($record['projectdatefrom']);
$date1 = date("Y/m/d", $datefrom);
Then I give the input vield the value="$date1.
Now I have another field for the H:i, so I'd like to have them seperate from each other.
Can can I cut the Y/m/d of the $date1 and only return the H:i?
Was trying doing things like this: $datetest = date("H:i", $datefrom); but no success.
$datefrom will need to be a UNIX timestamp. strtotime() can be useful for generating one off a plain-text or MySQL-style date string.
There is no need to escape the string after it was returned from the DB.
Therefore:
$date = date("Y/m/d", strtotime($record['projectdatefrom']));
$time = date("H:i", strtotime($record['projectdatefrom']));
Or using DateTime:
$dt = new DateTime($record['projectdatefrom']);
$date = $dt->format('Y/m/d');
$time = $dt->format('H:i');

timespamp convert 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);

Categories