Asked  7 Months ago    Answers:  5   Viewed   49 times

I Have a string that is equal to a date, represented as the number of milliseconds since the Unix epoch.

I am trying to output it into d-m-Y.

The string I was given was "1227643821310", and I am told that the result should be equal to 2-12-2008, but I keep getting a result of 25-11-2008

My code is as follows:

$mil = 1227643821310;
$seconds = $mil / 1000;
echo date("d-m-Y", $seconds);

Any ideas as to why this might be?

 Answers

70

You are already doing it right, 1227643821 is simply not 02-12-2008, it is indeed 25-11-2008.

Wednesday, March 31, 2021
 
HexaGridBrain
answered 7 Months ago
80

If you you install the PHP Internationalization Package, you can do the following:

IntlTimeZone::createTimeZone('America/New_York')->getDisplayName()

This will return the CLDR English standard-long form by default, which is "Eastern Standard Time" in this case. You can find the other options available here. For example:

IntlTimeZone::createTimeZone('Europe/Paris')->getDisplayName(true, IntlTimeZone::DISPLAY_LONG, 'fr_FR')

The above will return "heure avancée d’Europe centrale" which is French for Central European Summer Time.

Be careful to pass the first parameter as true if DST is in effect for the date and time in question, or false otherwise. This is illustrated by the following technique:

$tz = 'America/New_York';
$dt = new DateTime('2016-01-01 00:00:00', new DateTimeZone($tz));
$dst = $dt->format('I');
$text = IntlTimeZone::createTimeZone($tz)->getDisplayName($dst);
echo($text); // "Eastern Standard Time"

Working PHP Fiddle Here

Please note that these strings are intended for display to an end user. If your intent is to use them for some programmatically purpose, such as calling into another API, then they are not appropriate - even if the English versions of some of the strings happen to align. For example, if you are sending the time zone to a Windows or .NET API, or to a Ruby on Rails API, these strings will not work.

Friday, May 28, 2021
 
Norgul
answered 5 Months ago
51

You can use the datetime object or their function aliases for this:

Example (abridged from PHP Manual)

date_default_timezone_set('Europe/London');

$datetime = new DateTime('2008-08-03 12:35:23');
echo $datetime->format('Y-m-d H:i:s') . "n";
$la_time = new DateTimeZone('America/Los_Angeles');
$datetime->setTimezone($la_time);
echo $datetime->format('Y-m-d H:i:s');

Edit regarding comments

but i cannt use this method because i need to show date in different time zones as the user login from different locations

That's not a problem. When a user logs in, you determine his timezone and set it to your DateTime object just like shown. I'm using a similar approach in one of my projects and it works like a charm.

in the database i need to get the dates in any single timezone, then only it can be processed properly

You store the time either as a timestamp or a datetime in one timezone. When you query a DateTime field, you either convert the time in a DateTime object to this timezone or - if your db supports it - query with the selected timezone.

Tuesday, June 1, 2021
 
eek
answered 5 Months ago
eek
41

Start by converting your milliseconds to a TimeSpan:

var time = TimeSpan.FromMilliseconds(milliseconds);

Now, in .NET 4 you can call .ToString() with a format string argument. See http://msdn.microsoft.com/en-us/library/system.timespan.tostring.aspx

In previous versions of .NET, you'll have to manually construct the formatted string from the TimeSpan's properties.

Sunday, July 18, 2021
 
keisar
answered 3 Months ago
98

Use DateTime class to call function createFromFormat static function

$myDateTime = DateTime::createFromFormat('d/m/Y H:i', '15/09/2015 12:00');
$newDateString = $myDateTime->format('Y-m-d H:i');

Tested and giving Output:

2015-09-15 12:00
Saturday, October 2, 2021
 
Sibtain
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :